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
Benjamin Hedgepeth
5,672 Pointsdo...while loop running not in sequence
I have this loop
var loginName = 'Ben';
var loginCode = 'abc';
var loginCheck = true;
function loginAccess() {
do {
var loginAttempt = prompt("What is your name");
if (loginAttempt !== loginName) {
alert("Sorry, that's not it");
} else {
break
}
} while (loginCheck);
}
do {
var loginAttempt = prompt("What is your code");
if (loginAttempt !== loginCode) {
alert("Sorry, that's not it");
} else {
break
}
} while (loginCheck);
loginAccess();
Why is it that the code is being asked first instead of asking for the name? I'm wanting the alert dialog box to prompt for the user's name first.
1 Answer
andren
28,558 PointsYour first loop is within the loginAccess function, your second one is not. Any code within a function won't be run until the function is called, and since the function is called last in your code the loop within it is the last to run.
If you move the second loop within the function like this:
var loginName = 'Ben';
var loginCode = 'abc';
var loginCheck = true;
function loginAccess() {
do {
var loginAttempt = prompt("What is your name");
if (loginAttempt !== loginName) {
alert("Sorry, that's not it");
} else {
break
}
} while (loginCheck);
do {
var loginAttempt = prompt("What is your code");
if (loginAttempt !== loginCode) {
alert("Sorry, that's not it");
} else {
break
}
} while (loginCheck);
}
loginAccess();
Then you will end up with the desired result.