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 trialSharath Chandra
3,957 PointsCould someone please review the code.
I've tried to rewrite the instructors code in the do while loop video as follows. Could someone review the code please.
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 = parseInt(prompt("Guess the number"));
guessCount += 1;
} while ( guess !== randomNumber )
document.write("<h1>You guessed the number!</h1>");
document.write("<p>It took you " + guessCount + " attempts to guess the number " + randomNumber);
2 Answers
Steven Parker
231,261 PointsIt looks OK at first glance — are you having any trouble with it?
Sharath Chandra
3,957 PointsHi Steven. Thanks for your response. The code works fine. I've an other question in the code below. If I add the variable var html = "<ol>" inside the for loop, the only output I see is 4.Mango. However, if I add the variable var html = "<ol>" outside the loop, I see all the array items as an ordered list as expected. I'm having trouble understanding why placing the var html = "<ol>" inside the loop is causing to display just the last item in the Array.
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var html = "<ol>";
for (var i = 0; i < fruits.length; i++) {
html += "<li>" + fruits[i] + "</li>";
}
html += "</ol>";
document.getElementById("demo").innerHTML = html;
<script>
Steven Parker
231,261 PointsIf you place html = "<ol>"
inside the loop, then each loop will wipe out everything that was previously put into the "html" variable. So only the last one remains when the loop finishes.
Placing it before the loop makes sense for two reasons:
- it creates the variable and initializes it
- you only want one
<ol>
tag, at the beginning of the list
Balázs Buri
8,799 Pointsyou can remove the
var correctGuess = false;
line. it's an unused variable
Jose Gallardo
1,662 PointsJose Gallardo
1,662 PointsIt works properly. As an exercise I suggest that after every try you can give a little hint to the user.
For example, if the input number is lower than the winner number, print a message saying "The winner number is higher". And the same way when the input number is higher than the winner one.
Lastly, after 3 tries you can output a message saying "I'm sorry you didn't guess the number", and then the game ends.