JS - can't reset variable after while loop

Hi, I am having an issue with simply resetting a variable after the completion of a while loop that is inside of a do/while loop.

I would like to reset “x” back to 1 . However, no matter where I attempt to reset it in the code, it will crash. the issue lies within the minTorMaker() .

Why does adding x=1; after format = formatClone not work?

Codepen: https://codepen.io/cforecode/pen/zYvzWeV

Hi,

It’s because when you set x=1 your while (x < 9) goes into an infinite loop and causes your app to crash.

You have nested while loops, the nested loop is dependent of x which gets changed in the parent loop and while (x < 9) never quits because x always resets to 1.

Hope this explained it.

Thank you for your response. I am still a bit confused as I’ve done this nested loop before and the inner loop updated fine. I’ve stepped through this in chrome a few times. How then do I get x to update?

It looks like there are cases where n is never incremented inside the if/if else in the format.forEach block, so the while loop never stops.

Try making sure it get incremented no matter what inside the while loop. I didn’t really read the code to figure out what the expected output is.

Thanks for the replies. solved it. Was a reference error need to deep copy.