0
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)
2
  • 1
    timeCost.push(cost_per_minute[i]*ride_time) ... please have a look at the console first, it will tell you that [i]* is invalid Commented Jun 23, 2017 at 17:12
  • 1
    The length of the "cost_per_minute" value, is 4, so this loop is never going to run! Commented Jun 23, 2017 at 17:13

3 Answers 3

3

This is far more concise with .map():

let cost_per_minute...
const ride_time = 30;
let timeCost = cost_per_minute.map(x => x * ride_time);
Sign up to request clarification or add additional context in comments.

Comments

1

var ride_time = 30;
var cost_per_minute = [0.2, 0.35, 0.4, 0.45]
var timeCost = cost_per_minute.map(function(i){return i* ride_time})
console.log(timeCost)

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.