var timeCost =[];
var ride_time = 30;
var cost_per_minute = [0.2, 0.35, 0.4, 0.45];
for (let i = 0; i < cost_per_minute.length; i++ ){
timeCost.push(cost_per_minute[i]*ride_time)
}
console.log(timeCost)
-
1timeCost.push(cost_per_minute[i]*ride_time) ... please have a look at the console first, it will tell you that [i]* is invalidJonas Wilms– Jonas Wilms2017-06-23 17:12:02 +00:00Commented Jun 23, 2017 at 17:12
-
1The length of the "cost_per_minute" value, is 4, so this loop is never going to run!Iain J. Reid– Iain J. Reid2017-06-23 17:13:06 +00:00Commented Jun 23, 2017 at 17:13
Add a comment
|
3 Answers
Two places in your code:
The 2nd part of a for loop is commonly a condition of the counter (i.e. i) being less than the total count of the array (.ie. cost_per_minute.length)
for (var i = 0;
i < cost_per_minute.length;i++ ){
The syntax for an element within an array is nameOfArray[i] with i as the variable index number.
timeCost.push(
cost_per_minute[i]*ride_time)
}
var timeCost =[];
var ride_time = 30;
var cost_per_minute = [0.2, 0.35, 0.4, 0.45];
for (let i = 0; i < cost_per_minute.length; i++ ){
timeCost.push(cost_per_minute[i]*ride_time)
}
console.log(timeCost)