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 trialJoseph Chauvin
Full Stack JavaScript Techdegree Student 12,029 PointsWhy would the while loop run the code if the condition is 'false'? I thought that the answer would be only 'Goodbye'?
while (false) {
console.log('Hello');
}
console.log('Goodbye');```
1 Answer
Steven Parker
231,236 PointsYou are correct. Since the condition for the while loop is false
, the loop never runs and 'Hello' never prints. But the second console.log
does run because it's outside the loop.
Perhaps a stray mouse click caused a different answer to be selected? I've done that once or twice.
Joseph Chauvin
Full Stack JavaScript Techdegree Student 12,029 PointsJoseph Chauvin
Full Stack JavaScript Techdegree Student 12,029 PointsThe answer to the question is actually Hello and Goodbye, and the explanation is: "Even though the condition is false, a do..while loop always run at least once."
Steven Parker
231,236 PointsSteven Parker
231,236 PointsThere are two similar questions. The code shown above is from the question on the plain "while" and the answer is just "Goodbye" for the reasons I shared.
There's also a question about a "do...while" that has different code and a different answer. Note that the quiz questions are randomized and you may not get both of them in the same quiz session.
Joseph Chauvin
Full Stack JavaScript Techdegree Student 12,029 PointsJoseph Chauvin
Full Stack JavaScript Techdegree Student 12,029 PointsIt seems that both of those questions you mentioned have the wrong answers to them on the quiz
Joseph Chauvin
Full Stack JavaScript Techdegree Student 12,029 PointsJoseph Chauvin
Full Stack JavaScript Techdegree Student 12,029 Pointsnvm, I think it was bugged or something because now it has the right answers, thank you so much for your help