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

Why is my code not running?

Hey guys, I was following along the random game video in the JS basics course. Not sure why my code is not running. Can someone please check? https://w.trhou.se/3ne7w2ve7y

I don't see any prompts etc. at all.

Hey smriti chawla , You forget to close if conditions in one or two places which stopping your code from running.

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 ){
                var guessMore = prompt ('Sorry. The number is more than ' + parseInt(guess) + '. Guess again.');
                if (parseInt (guessMore) === randomNumber) {
                     correctGuess = true;
                 }
}else if ( parseInt (guess) > randomNumber ) {
                var guessLess = prompt ('Sorry. The number is less than ' + parseInt(guess) + '. Guess again.');
                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

I found a few syntax errors in your js code. Once i corrected them, it worked.

On lines 8 and 13 you had a space between your parseInt() method calls. Correct syntax is parseInt(someValue)

On line 11, you forgot your closing bracket before your else if

And finally on line 22, you forgot to end your initial if statement with a closing bracket.

This is a perfect example of why working with nested if statements can become hard to maintain. Im sure the next lesson is going to introduce switch statements for complex conditional logic.

Here is a fiddle with the corrected syntax... https://jsfiddle.net/p86yj19a/1/ Hope this helps :thumbsup:

There are some syntax error in your javascript code, I corrected your code, like missing bracket, sorry If there are some mistake in my correction, I am a newbie :D check here: https://w.trhou.se/mx7ybnx08i I also using button for user to click, so if he click the prompt will be fired