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 trialTaylor Steele
2,531 PointsMade 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>');
Steven Parker
231,271 PointsWhile you're fixing stuff, please quote your code block, using the instructions in the Markdown Cheatsheet pop-up below the answer area.
2 Answers
Steven Parker
231,271 PointsFor 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! I thought the phony "hint" idea was pretty funny.
Nah-Shon Alexander
2,305 PointsI had fun! thank you!
Taylor Steele
2,531 PointsTaylor Steele
2,531 PointsJust realised my randomHintDecider if block was a waste of time..