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
raykennedy2
11,391 Pointswhy does this happen?? quiz question?
quiz question?
do { console.log('Hello'); } while (false) console.log('Goodbye');
this reselts in Hello Goodbye being printed to the console,
i get that 'Hello' will be printed because the do while loop always runs once but does the while part always run once aswell or is something making this false? ( what is false in this/What would be true??)
1 Answer
Rastko Jovic
14,883 PointsAs you have stated correctly, the do while loop always executes the code in the DO section once.The second part is the while statement which evaluates the expression in the parenthesis and if the expression evaluates to true it continues the execution, otherwise it stops. Your loop has "false" in the parenthesis which always evaluates to False.In order for this to execute more than once, you could do something like :
do { something...}
while (1==1)
1==1 is True so the expression in parenthesis evaluates to True. In this particular case the expression always evaluates to true, because 1 will always equal 1 so this loop will continue forever.
Chris Davis
16,280 PointsChris Davis
16,280 PointsWouldn't your suggestion
while(1 == 1)create an infinite loop? Lols! Nevermind, just read the rest of your post! My bad!raykennedy2
11,391 Pointsraykennedy2
11,391 Pointsgreat reply, having just false instead of a condition threw me off, i get it now, thank you!