My code is freezing my browser

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?

  • Open DevTools (F12).
  • Go to Application (Chrome) or Storage (Firefox) tab
  • Expand Local Storage
  • https://freecodecamp.org
  • You can clear everything or…
  • Find the key “currentChallengeId”
  • Find that id under keys
  • Delete this entry
  • Now you should be able to navigate to the challenge and your previous code will not be loaded and your browser won’t crash.
1 Like

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.