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

JavaScript The Solution

Steve Gallant
Steve Gallant
14,943 Points

Slightly 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
Steven Parker
229,981 Points

Joel'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
Steve Gallant
14,943 Points

Thanks 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

i = Math.floor(Math.random() * 10);
while (i != 8) {
    text += i + ' ';
    i = Math.floor(Math.random() * 10);
}
text += i;

print(text)

Seems like you are saying nothing wrong with my version, which is what I wanted to check, so thanks!

Steve

Steven Parker
Steven Parker
229,981 Points

Yes, your version is a step between the instructor's two versions in regards to compactness. All three function exactly alike.