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

What is wrong with this JS code? Where is the Syntax Error?

<!DOCTYPE html> <html> <head> </head> <body> <script>

        var guess = prompt("I am guessing a number between 1 and 100. What is it?");
        var actualNumber = 43;

        if (parseInt(guess) === 43) {
        alert("You guessed it!")
        }

        if else ( parseInt(guess) < 43 || parseInt(guess) > 37 ) {
        alert ("You are close!")
        }

        if else ( parseInt(guess) > 43 || parseInt(guess) < 49 ) {
        alert("You are close!")
        }

        else {
        alert("You are way off!")
        }




    </script>

</body>

</html>

2 Answers

The main problem with your code is that in javascript conditional else statements are called "else if" not "if else" as you have typed.

Also there is a slight logic error with you statements, with the way they are setup, if I were to enter say 1000 as a guess your program would respond with "You are close!". This is because the first else if statement checks to see if the guess is below 43 or above 37. 1000 being a number far higher than 37 fulfills that condition and therefore that statement runs.

If you change the or (||) part of your statements to be and (&&) then your code functions as I suspect you intended it to.

Here is the fixed code with those two issues addressed:

var guess = prompt("I am guessing a number between 1 and 100. What is it?");
var actualNumber = 43;

if (parseInt(guess) === 43) {
  alert("You guessed it!")
}

else if (parseInt(guess) < 43 && parseInt(guess) > 37 ) {
  alert ("You are close!")
}

else if (parseInt(guess) > 43 && parseInt(guess) < 49 ) {
  alert("You are close!")
}

else {
  alert("You are way off!")
}

Also you can simplify your else if statements by combining your two "You are close!" statements into one statement like this:

else if (parseInt(guess) < 49 && parseInt(guess) > 37 ) {
  alert("You are close!")
}         

While this range (lower than 49 and higher than 37) does include the right answer (43) that is not an issue since the else if statement will only be looked at if the first if statement was not executed. The first if statement check to see if the answer is 43, that means that if 43 was entered then the else if statement is never run even though the answer technically fits into it's condition.

Also as mentioned by another poster it's generally a good idea to get into the habit of adding semi-colons to all of your statements, but they are not actually mandatory (in Javascript) unless you are writing multiple statements on the same line. So for your script the lack of semi-colons will not result in any errors.

Thank you! Best Answer

Don't feel bad! It's the most common error that programmers probably ever run into -- missing semi-colons. I know professional developers who have been writing code for years (even decades) and they still have this happen to them.

alert("You guessed it!");

That's one of them, see if you can find the others on your own! It's an important lesson to pay attention to every character while coding. Computers can't make assumptions for us!

edit: Looks like andren pointed out some other errors!

Wrong. Semicolons are optional in JavaScript. Most of the time people use semicolons, though.

It is best practice to use semicolons, but it isn't required.

~Alex