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 trialAbhinav Dhiman
4,989 PointsBummer your code takes too long to run
Pls help..
var secret = prompt("What is the secret password?");
while( secret ==='sesame'){
document.write("You know the secret password. Welcome.");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
2 Answers
Bob Boursaw
2,047 PointsI am pretty new to this, but thought I would provide my thoughts based on the above code.
In the 'while' statement, you have 3 equals signs but believe it should be just 2. = is assignment while == equated to "is".
Also, does the while condition need to have the conditional portion within ()?
while (secret == sesame) { // do this }
I may be off base here, so if I am, my apologies.
Ricardo Hill-Henry
38,442 PointsYour code takes to long to run, because your loop never ends if the user types in "sesame". They'll get a page with "You know the secret password. Welcome." being printed endlessly.
The way the question is written sorta tricks you up. You aren't necessarily worried about what the code does when the secret is "sesame". You should actually be checking for when secret is not "sesame". In this case, if secret is not "sesame" you want to keep asking for the secret. If secret is sesame, you want to stop asking.
var secret; //you could also add a prompt here if you'd like
while( secret !=='sesame'){
secret = prompt("What is the secret password?");
}
Ricardo Hill-Henry
38,442 PointsRicardo Hill-Henry
38,442 PointsYou were getting closed towards the end. However, Javascript does include a comparison operator that uses === called strict equality. The double equal sign is called loose equality. To see the difference, open your console and try the following:
When using the double equal sign, Javascript does automatic type conversion. So instead of evaluating a string with "0" with an integer of 0, Javascript converts that string to it's numerical equivalent.