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 trialSundeep Singh
1,565 PointsPlease help!! My code asks for prompt but after that remains blank. Don't know what mistake I have done?
https://teamtreehouse.com/workspaces/8747702
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
} else if ( pasrseInt(guess) < randomNumber ) {
var guessMore = prompt(' Try again.I am thinking of a number more than ' + guess);
if(parseInt(guessMore) === randomNumber){
correctGuess =true;
}
} else if ( pasrseInt(guess) > randomNumber ) {
var guessLess = prompt(' Try again.I am thinking of a number less than ' + guess);
if(parseInt(guessLess)=== randomNumber) {
correctGuess = true;
}
}
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
rydavim
18,814 PointsOops! We can't view your workspace using the same link you use. Your workspace is unique to you.
In order to share your workspace with us, you'll need to use snapshots. You can create a snapshot using the camera icon in the top right of your workspace. That link you can post here and we'll be able to see all your code and (hopefully) help you out!
Sundeep Singh
1,565 PointsThis is the code I have , if any one can point my mistake that will be great
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
} else if ( pasrseInt(guess) < randomNumber ) {
var guessMore = prompt(' Try again.I am thinking of a number more than ' + guess);
if(parseInt(guessMore) === randomNumber){
correctGuess =true;
}
} else if ( pasrseInt(guess) > randomNumber ) {
var guessLess = prompt(' Try again.I am thinking of a number less than ' + guess);
if(parseInt(guessLess)=== randomNumber) {
correctGuess = true;
}
}
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
2 Answers
rydavim
18,814 PointsLooks like you have a couple typos in your code. There are two instances I found where parseInt was mistyped. I think that's the only thing causing issues in your code. Works for me now, nice job!
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber) {
correctGuess = true;
} else if (parseInt(guess) < randomNumber) { // There was a typo in parseInt.
var guessMore = prompt('Try again. I am thinking of a number more than ' + guess);
if (parseInt(guessMore) === randomNumber){
correctGuess =true;
}
} else if (parseInt(guess) > randomNumber) { // There was a typo in parseInt.
var guessLess = prompt('Try again. I am thinking of a number less than ' + guess);
if(parseInt(guessLess) === randomNumber) {
correctGuess = true;
}
}
if (correctGuess) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
console.log("The guess was: " + guess);
console.log("The random number was: " + randomNumber);
Sundeep Singh
1,565 Pointsthanks :)
Caesar Bell
24,829 PointsOkay it works now, I tried it. You can clean your code up a little and not have so many nested if statements, also you might want to look into using functions it cleans up your code a lot and its more reusable. Coding is all about using reusable code and keeping your code "dry".
Updated code:
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
} else if (parseInt(guess) < randomNumber ) {
guess = prompt(' Try again.I am thinking of a number more than ' + guess);
}else if ( parseInt(guess) > randomNumber ) {
guess = prompt(' Try again.I am thinking of a number less than ' + guess);
}
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
Ray Wai
2,355 PointsHate to tell you that this code will not work correctly
Caesar Bell
24,829 PointsCaesar Bell
24,829 PointsCan you post your code in here, the workspace link doesnt exisit anymore