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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Alternative condition - does it work as well?

Hey guys, I went ahead and tried to do the challenge before having a look at the instructor's take on it and came up with this. Basically, it makes the loop run until the two numbers are alike. Is it as good, or are there possible downsides? Technically, it should be the same.

var randomNumber = getRandomNumber(10); var guess; var howManyGuesses= 0;

function getRandomNumber( upper ) { var num = Math.floor(Math.random() * upper) + 1; return num; }

do { guess = prompt("Guess a number between 1 and 10!"); parseInt(guess); howManyGuesses++;

} while (guess !== randomNumber )

document.write("The random number was" + randomNumber + "and you took this many tries to guess it right: " + howManyGuesses);

1 Answer

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Lara,

I looked over your code and while it was a valiant attempt, there are some issues. I won't actually go through them here though, since I'm sure you'll run across the answers when you look at the videos for the solution to the challenge.

One thing I did feel I should point out is that you were missing a semicolon after your while statement, which is a game breaking issue in your code. Beyond that, the issues were just what goes where and when it seemed. You're definitely on the right track.

I did want to point you in the right direction when it came to showing your code though, since I've seen your code in a few posts now.

How to Post Code on the Forums

There are two ways to share your code on the forums here, excluding using external tools from outside of Treehouse.

The first method is to use a series of three ` (backticks, located at the top left of the keyboard) without any spaces on one line, paste all of your code starting on the second line, then closing the code block with a second series of three backticks. Take a peek at the link for the "Markdown Cheatsheet" located above the "Post Answer" button anytime you're about to post a comment or answer.

The second method is a little more convoluted, but it lets us look at your entire project and make our own copy to try fixing issues. I'll order the steps since it can be a little confusing the first time around:

  1. Open the workspace you're having trouble with.

  2. On the menu bar located to the left of the "Preview Workspace" button is the "Snapshot Workspace" (camera icon), click that.

  3. A popout will appear with another button that says "Take Snapshot", click that.

  4. It should create a new snapshot, which will appear beneath the "Take Snapshot" button in the popout. Click the snapshot you want to share.

  5. The snapshot should open a new browser tab. Highlight the url in the snapshot's browser tab (it should look like this: https://w.trhou.se/ducj79b1i0 ).

  6. Create your forum post and paste the snapshot's url into the post. Other Treehouse users will be able to open the snapshot, then 'fork' a new copy to check out.

Keep in mind that you can only ever have five snapshots, but you can delete them by hovering over the snapshot located below the "Take Snapshot" button in the popout and click the trash bin icon.

Good luck!

Daniel, you said..."you were missing a semicolon after your while statement, which is a game breaking issue in your code. " Is it really game breaking? cause it works without it. It might irritate some people but that's about it. Well maybe gain some scorn in the forums as well.

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey John,

You're right, it would work, but knowing where semicolons should and shouldn't be placed is the game breaking part here. Seeing as most people reading these topics are learning to code in various languages, I value promoting good practices over pointing out where corners could be cut seeing as it's easy to get confused about syntax while learning.

I'll defer the conversation to this one a few other student had about this very topic ten months ago:

https://teamtreehouse.com/community/-do-while-syntax-with-a-semicolon

Have a great night!

Daniel, hey, I agree that for readability semicolons need to go where they are EXPECTED to go. I just thought calling a "deal-breaker" was a bit of a hyperbolic overstretch. I don't know that much so I like to pick at the details. And YOU have a good night as well :D