samedi 21 décembre 2019

accessing array on if statements

i have a code like this :

function firstArrived(cars) {
  // code below here
  var yellow = [];
  var red = [];
  var  black = [];
  for(var i=0; i <= cars.length; i++){
       if(cars[i][1] === 'yellow'){
      yellow.push(cars[i][0]);
    }
    else if(cars[i][1] === 'red'){
      red.push(cars[i][0]);
    }
    else{
      black.push(cars[i][0]);
    }
  }
return yellow;
};

//TEST CASE

console.log(firstArrived([['1171 CA', 'yellow'],['1234', 'black'],['V 76998', 'red']]));

the code is supposed to have a result like this :

[ '1171 CA' ] 

but instead, i've got an error like this:

Uncaught TypeError: Cannot read property '1' of undefined

can you help me to find whats wrong in my code? thanks a lot.

Aucun commentaire:

Enregistrer un commentaire