Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through array, print with string

I have a simple array like:

var myArr=["one","two","three"];

an I have a counting loop, which increases the value of var i by one.

What I want to do is print the next value from the array each time the loop runs, next to a text string, like so:

alert('value number '+myArr[i]+);

But for some reason I can't get this to work. The following code works, so I'm assuming I'm not calling the counter right:

alert('value number '+myArr[0]+);
like image 698
Adam Tal Avatar asked Mar 22 '26 04:03

Adam Tal


1 Answers

Make sure that you're properly incrementing the loop counter (it should start at zero and run until the highest index in the array), and that the stray + at the end of your alert line is removed.

for (var i = 0; i < myArr.length; ++i) {
  alert('value at index [' + i + '] is: [' + myArr[i] + ']');
}
like image 150
John Feminella Avatar answered Mar 24 '26 17:03

John Feminella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!