0

I have declared a constructor in javascript. And I want to call that with different objects. But I want to change the name of the references by using loop but not manually.

I am trying this below code but seems an error in function CalculateTimeForEachOrbit.

Code:

let i = 1, totalOrbits = 6;

function CalculateTimeForEachOrbit (time) {
    this.time = time;
 }

  while (i <= totalOrbits)   {
         let orbit = "orbit";
         orbit = orbit + i;    
         const orbit+"bikeTime"  = new CalculateTimeForEachOrbit(10);
         const orbit+"tuktukTime"  = new CalculateTimeForEachOrbit(20);
         const orbit+"carTime"  = new CalculateTimeForEachOrbit(30);
    }

I want to set the time for each 6 orbits for every vehicle via loop instead of writing the object 6 times.

Final Output Expected:

Orbit1BikeTime :{
time : 10
}


Orbit1TuktukTime :{
time : 20
}

Orbit1CarTime :{
time : 20
}

Orbit2BikeTime :{
time : 10
}


Orbit2TuktukTime :{
time : 20
}

Orbit2CarTime :{
time : 20
}

Like this till Orbit6.

Biku7
  • 450
  • 6
  • 16

0 Answers0