Create a function called ‘arrayReverser’ that takes in one parameter, ‘arr’. Inside of arrayReverser, create an empty array called ‘reversed’. Using a for loop, loop over the passed in array IN REVERSE (this means your counter should decrement), and then add each item to the ‘reversed’ array variable you created. Finally, return the ‘reversed’ array variable.
//Code Here
function arrayReverser(arr){
let reversed = [];
for(let i = 0; i < arr.length-1; i--){
reversed.push('hello')
}
return reversed;
}
I think I wrote an infinite loop and it froze my browser, how would I fix this?
You will know your answer once you understand your for loop, for loop consist of three statements, Right? first is starting condition i.e in your case ‘let i=0’, second is loop ending condition i.e ‘i < arr.length’, and last condition is which way you want to traverse. So, you only need to swap the conditions i.e for(let i = arr.length-1; i = 0; i–), so that i is set to last character of array and ends at first character of array. For example: if array contains ‘H E L L O’ string. loop starts from character ‘O’ i.e array index = 4 (i = arr.length-1) and loop end at array index = 0 (i = 0). I’ve explained this, keeping in mind you have understood Array concept. If not, learn from internet. OR I don’t mind replying back.