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 JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Prioritising page reload before other prompts occur.

Please see JavaScript below.

I would like the page to reload if the user enters any other than yes by using a conditional statement that causes the page to reload. However when I add the prompts, the reload occurs once all of the prompts have been cycled through.

How can I prioritise the reload of the page?

Any other help would be appreciated.

//Introduction
var userReady = prompt("Hi! It's your quiz master here. Are you ready for your questions?");
if (userReady.toUpperCase() === "YES" ) {
  alert("Okay cool, please hit return and lets begin. Good luck!") ;
} else {
  alert("Okay, please enter yes when you are ready.");
  location.reload();
}

//Recording Variables
var questionNumber = 1 ;
var questionsAnswered = 0 ;

//Question Variables and Questions
var questionOne = prompt("Question number " + questionNumber + ": When was Mt. Everest first climbed?" + "\nYou have answered: " + questionsAnswered + "/5 questions.");

2 Answers

Steven Parker
Steven Parker
231,269 Points

You could re-organize your code.

If you move the remainder of the program into the same code block as the "OK, cool..." message, then the reload would be the last statement of the program and take effect immediately.

But why reload the page only to repeat the greeting and first question? You could just place them in a loop:

//Introduction
do {
  var userReady = prompt("Hi! It's your quiz master here. Are you ready for your questions?");
  var ready = userReady.toUpperCase() === "YES";
  if (!ready)
    alert("Okay, please enter yes when you are ready.");
} while (!ready);
alert("Okay cool, please hit return and lets begin. Good luck!");

// rest of program....

I have yet to learn about loops so perhaps I was getting little ahead of myself.

Thank you Steven much appreciate!????