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

why do you increase it by 1 += 1; for a guess? just wondering

do { guess = prompt("I am thinking of a number between 1 and 10. What is it?"); guess += 1; } while ();

3 Answers

Steven Parker
Steven Parker
243,656 Points

This code doesn't seem to do anything useful, it will just keep asking for input forever. The part that increases the amount doesn't really affect what the program does overall.

Is this part of a course? Please provide a link to the course page you are working with.

yes sorry here's the full version

=====================================

var randomNumber = getRandomNumber(10); var guess; var guessCount = 0; var correctGuess =false;

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

do { guess = prompt("I am thinking of a number between 1 and 10. What is it?"); guessCount += 1; if (parseInt(guess) === randomNumber) { correctGuess= true;

Steven Parker
Steven Parker
243,656 Points

When posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: Or watch this video on code formatting.

Steven Parker
Steven Parker
243,656 Points

The second code doesn't seem complete either, but there's enough there to tell that guessCount is being used to keep track of how many guesses are made. So for each guess, it is increased by 1.

Then, after the loop ends, guessCount will have the number of times the loop repeated.