# JS - can't reset variable after while loop

**URL:** <https://forum.freecodecamp.org/t/js-cant-reset-variable-after-while-loop/380987>\
**Category:** General\
**Created:** [April 29, 2020, 3:46pm UTC](https://forum.freecodecamp.org/t/js-cant-reset-variable-after-while-loop/380987 "2020-04-29T15:46:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![codev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codev/32/145607_2.png) [@codev](https://forum.freecodecamp.org/u/codev)\
**Post date:** [April 29, 2020, 3:46pm UTC](https://forum.freecodecamp.org/t/js-cant-reset-variable-after-while-loop/380987/1 "2020-04-29T15:46:57Z")

</div>

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](https://codepen.io/cforecode/pen/zYvzWeV)

---

<div class="post-metadata">

**Author:** ![banafshr](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@banafshr](https://forum.freecodecamp.org/u/banafshr)\
**Post date:** [April 29, 2020, 4:10pm UTC](https://forum.freecodecamp.org/t/js-cant-reset-variable-after-while-loop/380987/2 "2020-04-29T16:10:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![codev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codev/32/145607_2.png) [@codev](https://forum.freecodecamp.org/u/codev)\
**Post date:** [April 29, 2020, 4:28pm UTC](https://forum.freecodecamp.org/t/js-cant-reset-variable-after-while-loop/380987/3 "2020-04-29T16:28:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [April 29, 2020, 4:47pm UTC](https://forum.freecodecamp.org/t/js-cant-reset-variable-after-while-loop/380987/4 "2020-04-29T16:47:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![codev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codev/32/145607_2.png) [@codev](https://forum.freecodecamp.org/u/codev)\
**Post date:** [April 30, 2020, 1:31am UTC](https://forum.freecodecamp.org/t/js-cant-reset-variable-after-while-loop/380987/5 "2020-04-30T01:31:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:56pm UTC](https://forum.freecodecamp.org/t/js-cant-reset-variable-after-while-loop/380987/6 "2021-06-01T18:56:09Z")

</div>

Closed on June 1, 2021, 6:56pm UTC


