Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can you reload a program without resetting the variables?

I am working on the intro to javascript game, and I knew a little more than was taught, so I went farther with the game, making the coins and poison and other items randomly spread on the page. Now, I am trying o make it so that when you win, the currentScore is reset, the winningScore increases by ten, the level increases by 1, and the rest of the program is rerun so the poison and stuff is in different positions. I tried window.location.reload(); but it reset all to variables when it reloads, so it makes you replay the same level over again. Is there a way to keep like 3 lines of code from reloading when the rest of the program reloads?

Here is the code for when you win: if (won) { won = false; nextLevel = true; } if (nextLevel) { window.location.reload(); winningScore += 10; currentScore = 0; level++; }

1 Answer

Steven Parker
Steven Parker
229,732 Points

You could put the things you want to keep into local storage, but it might be easier just to reset and re-randomize the things that should change instead of reloading.