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
Alexander D
8,685 PointsA loop with two exits, is that possible?
Hello there,
I am working through a simple program, but I do not quite understand how to break through the loop.
The program is asking you to guess a number between 1 to 10. If you get it right, you break the loop. The problem is that I want to give a maximum of 10 guesses, and if you get to 0, you loose.
At this point, it would be like a loop with two exit tunnels, but I do not know if the while-do loop allows that. Maybe I have to rewrite the program in a different way, but I am looking to suggestions.
In the program below I am revealing the random number, so that it's easier to guess:
var TriesLeft = 10;
var guess = " ";
var Random = getRandomNumber(10);
function getRandomNumber (upper) {
return Math.floor( Math.random() * upper ) + 1;
}
do {
guess = prompt("I am thinking of a number between 1 and 6. What is it? " + Random + " you have left " + TriesLeft + " attempts to get it right");
TriesLeft = TriesLeft -1;
if (parseInt(guess) === Random ) {
} else if (isNaN(parseInt(guess))) {
alert("This is not a number, please refresh and try again!");
} else if (parseInt(guess) < Random ) {
alert("The random number is higher, try again");
}else if (parseInt(guess) > Random ) {
alert("The random number is lower, try again");
}
} while (parseInt(guess) !== Random );
document.write("You got it!");
Is it possible to position another "while" statement? or an "else". I am just afraid that they will be inside the loop, so it won't work.
Thanks!
2 Answers
Juan Lopez
Treehouse Project ReviewerYou can break; out of a loop if you like. So in your code above you could do a conditional like
if (TriesLeft === 0){
document.write("Sorry you ran out of lives :-( ");
break;
}
Here is a link with more info.
Alexander D
8,685 PointsSolution here for anyone stumbling upon this post.
To solve this, I simply left the while blank, and added two break points, which represent my exits:
var TriesLeft = 3;
var guess = " ";
var Random = getRandomNumber(10);
function getRandomNumber (upper) {
return Math.floor( Math.random() * upper ) + 1;
}
do {
guess = prompt("I am thinking of a number between 1 and 6. What is it? " + Random + " you have left " + TriesLeft + " attempts to get it right");
TriesLeft = TriesLeft -1;
if (parseInt(guess) === Random ) {
} else if (isNaN(parseInt(guess))) {
alert("This is not a number, please refresh and try again!");
} else if (parseInt(guess) < Random ) {
alert("The randon number is higher, try again");
}else if (parseInt(guess) > Random ) {
alert("The randon number is lower, try again");
} if (TriesLeft === 0){
document.write("Sorry you ran out of lives :-( ");
break;
}if (parseInt(guess) === Random){
document.write("you win ");
break;
}
} while (parseInt(guess) !== Random );
Alexander D
8,685 PointsAlexander D
8,685 PointsHello Juan,
That's probably the element I was missing. I haven't seen yet, it will probably appear down the course.
I've tried placing it inside, when I do this, I am able to break out, but I am getting two messages this time:
"Sorry you ran out of lives :-( " and "You got it!".
Is there any recommended position for the break statement to work?
Thanks for your time!