Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Build a Random Number Guessing Game

Made a version of this game. Let me know how it works...

var randomNumber = Math.floor(Math.random() * 10) + 1; 
var randomHintDecider = Math.floor(Math.random() * 10) + 1;
if (randomHintDecider === 1) {
    var hintNumber = 1
} else if (randomHintDecider === 2) {
    var hintNumber = 2
} else if (randomHintDecider === 3) {
    var hintNumber = 3
} else if (randomHintDecider === 4) {
    var hintNumber = 4
} else if (randomHintDecider === 5) {
    var hintNumber = 5
} else if (randomHintDecider === 6) {
    var hintNumber = 6
} else if (randomHintDecider === 7) {
    var hintNumber = 7
} else if (randomHintDecider === 8) {
    var hintNumber = 8
} else if (randomHintDecider === 9) {
    var hintNumber = 9
} else {
    var hintNumber = 10
}
alert('I have chosen a whole number between 1 and 10. Can you guess it in two turns?');
var guessedNumber1 = parseInt(prompt('What is your first guess?')); 
if (randomNumber === guessedNumber1) {
    document.write('<h2>Well done you guessed correctly.</h2>')
} else {    
    if (guessedNumber1 > randomNumber && guessedNumber1 !== 2 && guessedNumber1 !== 10) { 
        if (hintNumber >= guessedNumber1) {
            hintNumber = 1
        }
        var guessedNumber2 = parseInt(prompt('Unlucky, guess again. This is your final chance. What is your guess? Hint: The number I am thinking of is lower than a ' + guessedNumber1 + '. I also promise its not a ' + hintNumber + '... Or is it...'))
    } else if (guessedNumber1 < randomNumber && guessedNumber1 !== 9 && guessedNumber1 !== 1) {
        if (hintNumber <= guessedNumber1) {
            hintNumber = 10
        }
        var guessedNumber2 = parseInt(prompt('Unlucky, guess again. This is your final chance. What is your guess? Hint: The number I am thinking of is higher than a ' + guessedNumber1 + '. I also promise its not a ' + hintNumber + '... Or is it...'))
    } else if  (guessedNumber1 === 9 || guessedNumber1 === 2 || guessedNumber1 === 1 || guessedNumber1 === 10) {
        var guessedNumber2 = parseInt(prompt('Unlucky, guess again. This is your final chance. What is your guess? Hint: I promise its not a ' + hintNumber + '... Or is it...'))
    } 
    if (randomNumber === guessedNumber2 && randomNumber === hintNumber) {
        document.write('<h2>Arggh, your too clever for my tricks. Well done you guessed correctly, the number was a ' + hintNumber + '.</h2>')
    } else if (randomNumber === guessedNumber2 && randomNumber !== hintNumber) { 
        document.write('<h2>Arggh you got the right number! I knew I shouldn\'t have gone with a ' + randomNumber + '. At least I didnt lie to you.</h2>')
    } else if ((randomNumber !== guessedNumber2) && (randomNumber === hintNumber)) {
        document.write('<h2>Mwahaha, you should never trust me. My number was in fact a ' + hintNumber + '!</h2>')
    } else if ((randomNumber !== guessedNumber2) && (randomNumber !== hintNumber) && (guessedNumber2 === hintNumber)) {
        document.write('<h2>Wow! Do you not trust me. My number was not a ' + guessedNumber2 + '.' + ' It was in fact a ' +         randomNumber + '.</h2>')
    } else if ((randomNumber !== guessedNumber2) && (randomNumber !== hintNumber) && (guessedNumber2 !== hintNumber)) {
        document.write('<h2>Unlucky. My number was not a ' + guessedNumber2 + '.' + ' It was in fact a ' + randomNumber + '.</h2>')
    }
}
document.write('<h3>Refresh this page to try again.</h3>');

Just realised my randomHintDecider if block was a waste of time..

Steven Parker
Steven Parker
215,940 Points

While you're fixing stuff, please quote your code block, using the instructions in the Markdown Cheatsheet pop-up below the answer area. :arrow_heading_down:

2 Answers

Steven Parker
Steven Parker
215,940 Points

For the code itself, I have a few suggestions:

  • never declare a variable (using "var") more than once. Declare it in the outermost scope it will be used in.
  • there a several opportunities to make the code more compact without compromising the functionality.
  • when posting code, use the proper markdown so it displays correctly.

But overall, good job! :+1: I thought the phony "hint" idea was pretty funny. :smile: