How do I add an array?

Asked

Viewed 44 times

0

Good afternoon is so my doubt is imagining that I have these variables :

var powerRangers = ['Red', 'Black', 'Yellow', 'Pink', 'Blue'];

and when I use the is ( for example )

for (var i = 0; i < powerRangers.length; i++) {
     console.log([i]+":"+powerRangers);
}

The goal and exit : 0: Red' 1:black' 2:Yellow'

... And always so ... Help

And the real thing is :

0:Red, Black, Yellow, Pink, Blue 1:Red, Black, Yellow, Pink, Blue 2:Red, Black, Yellow, Pink, Blue 3:Red, Black, Yellow, Pink, Blue 4:Red, Black, Yellow, Pink, Blue

  • That’s not adding one array, you want to concatenate a numerical iterator with an element of a array in a string.

  • Ahhh Thank you !

1 answer

0


Just access your index array, thus:

 console.log([i]+":"+powerRangers[i]);
  • 1

    My God thank you so much ! D

  • 1

    In this case the correct code would be : for (var i = 0; i < powerRangers.length; i++) { console.log(i+":"+powerRangers[i]); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.