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 Basics (Retired) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

Would this .js code run properly?

Hello,

I am following through on the 'Improving the Random Number Game'. I tend to try and build before following the video to the end and came up with the following code. This code is intended to give the visitor 3 tries at guessing the randomNumber. Could someone tell me if this code is properly formatted? The reason I ask is that in Workspaces, it runs as intended, with three guesses and accompanying alert boxes counting down the # of tries a visitor has. However, when I copied my code over to another website that I use to tinker with code - Glitch.com - it seems to be running the program twice regardless whether I actually successfully guessed the randomNumber or not.

var randomNumber = Math.floor(Math.random() * 6 ) + 1; var numberofguesses = 3

var guess = prompt('I am thinking of a number between 1 and 6. What is it?');

if (parseInt(guess) === randomNumber) { alert("Correct!, you have guessed the right number"); document.write("<p>" + guess + " is the correct number.</p>"); } else if (parseInt(guess) > randomNumber) { alert(guess + " is higher than the correct number!"); } else if (parseInt(guess) < randomNumber) { alert(guess + " is lower than the correct number!"); }

if (parseInt(guess) !== randomNumber) { numberofguesses = numberofguesses - 1; alert("You guessed wrong, you have " + numberofguesses + " guesses left!"); var guessagain = prompt('Take a second guess, I am thinking of a number between 1 and 6.'); }

if (parseInt(guessagain) === randomNumber) { alert("Correct!, you have guessed the right number"); document.write("<p>" + guessagain + " is the correct number.</p>"); } else if (parseInt(guessagain) > randomNumber) { alert(guessagain + " is higher than the correct number!"); } else if (parseInt(guessagain) < randomNumber) { alert(guessagain + " is lower than the correct number!"); }

if (parseInt(guessagain) !== randomNumber) { numberofguesses = numberofguesses - 1; alert("You guessed wrong, you have " + numberofguesses + " guess left!") var guessthrice = prompt('Take one last guess, I am thinking of a number between 1 and 6.'); }

if (parseInt(guessthrice) === randomNumber) { alert("Correct!, you have guessed the right number"); document.write("<p>" + guessthrice + " is the correct number.</p>"); } else if (parseInt(guessthrice) > randomNumber) { alert(guessthrice + " is higher than the correct number!"); } else if (parseInt(guessthrice) < randomNumber) { alert(guessthrice + " is lower than the correct number!"); }

if (parseInt(guessthrice) !== randomNumber) { alert("Sorry, you have run out of guesses and have not guessed the correct number!"); document.write("<p>Better luck next time and thanks for playing!</p>"); }

2 Answers

https://w.trhou.se/gu46yrowo9

I believe I am using the Snapshot properly.

Disregard. I figured it out!

I removed the if (parseInt(guess) !== randomNumber) { } instances in my code and it runs smoothly without trouble. I've tested it a couple of times even on Glitch.com and it works fine now.