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 trialSteve Gallant
14,943 PointsSlightly DRYer, but any caveats?
My code for the fifth While loop exercise is shown below and seems to work properly from what I can tell. Joel had slightly more code than this in the video and reversed the order of lines within the loop. Any hidden issues with my version? thanks! Steve
i = 0;
while (i != 8) {
i = Math.floor(Math.random() * 10);
text += i + ' ';
}
print(text);
1 Answer
Steven Parker
231,275 PointsJoel's version has slightly less code (by one line) since the "do" style loop doesn't require the value of "i" to be preset like your ordinary "while" does. Also, both loops require "text" to be preset to an empty string but that's not shown in the code above.
Otherwise, the code content inside the loop appears to be the same (including the order), so the functionality will be identical.
Steve Gallant
14,943 PointsSteve Gallant
14,943 PointsThanks for your response Steven! Just to be clear, I am comparing my included code directly to Joel's solution for the "while" loop (not do-while) fifth example, which I am now copying in below:
Joel's code solution
Seems like you are saying nothing wrong with my version, which is what I wanted to check, so thanks!
Steve
Steven Parker
231,275 PointsSteven Parker
231,275 PointsYes, your version is a step between the instructor's two versions in regards to compactness. All three function exactly alike.