0
How do I loop through an array backwards?
https://code.sololearn.com/W35UHQQ4qNae/?ref=app I have a function that accepts an array as an argument. I want the function to put the array in a for loop that counts down and pushes each value in the array, from last to first, into a separate array. Then I want the function to alert the array. So basically I want a function that reverses the order of an array. I tried doing this but it keeps returning undefined. I think the problem is with the for loop counting down, but I’m not sure. Help is appreciated
1 Odpowiedź
+ 1
The problem is in logical operation (i <= 0) and the initial value of loop (arr.length).
For solve try:
1) i >= 0 on place that i <= 0 - This to correct the error.
2) arr.length - 1 on place that arr.length - This to insure that on start the array not have undefined value
The your code with the problems solved
https://code.sololearn.com/WfpoaV2Aarq6/?ref=app
(Obs: sorry by my bad english)