Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,149 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!????