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 JavaScript Basics (Retired) Making Decisions with Conditional Statements Combining Multiple Tests Into a Single Condition

Is there any syntax error ?

is there any error in my code.

Can you please provide your 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;
} 
 elseif(parseInt(guess)>randomNumber) {
    guess = prompt('Try Again, the number you entered was biggger than random one');
    if(guess === randomNumber){

        correctGuess = true;

    }
}
}elseif(parseInt(guess)<randomNumber) {
    guess = prompt('Try Again, the number you entered was smaller than random one');
    if(guess === randomNumber){

        correctGuess = true;

    }

}
if ( correctGuess ) {
    document.write('<p>You guessed the number!</p>');
}

5 Answers

There are a couple of things that need to be changed. For starters, your else ifs are incorrect. you have them as elseif when they should be two separate words. Secondly, you have an additional curly brace that needs to be deleted.

Try this.

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, the number you entered was biggger than random one');
  if (guess === randomNumber) {

    correctGuess = true;

  }
} else if (parseInt(guess) < randomNumber) {
  guess = prompt('Try Again, the number you entered was smaller than random one');
  if (guess === randomNumber) {

    correctGuess = true;

  }

}
if (correctGuess) {
  document.write('<p>You guessed the number!</p>');
}

I hope this helps.

// could you plz check this code also.
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){

    document.write('you got it');

} else if(parseInt(guess) > randomNumber){

    var an_Guess = prompt('u choose it bigger than random one. Try Again');

    if(parseInt(an_Guess)=== randomNumber){

        document.write('got it right');
    }
    else if(parseInt(an_Guess)> randomNumber){

        document.write('this time u choose it bigger. Try smaller one');

    }
    else if(parseInt(an_Guess)< randomNumber)
        {

        document.write('this time u choose it smaller. Try bigger one');

    }
}else(parseInt(Guess)< randomNumber){

    document.write('this time u choose it smaller. Try bigger one');
}

// could you plz check this code also.

Your final conditional statment is missing the if to complete the else if. an else statement can not have conditions unless it is an if or else if

// could you plz check this code also.
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) {

  document.write('you got it');

} else if (parseInt(guess) > randomNumber) {

  var an_Guess = prompt('u choose it bigger than random one. Try Again');

  if (parseInt(an_Guess) === randomNumber) {

    document.write('got it right');
  } else if (parseInt(an_Guess) > randomNumber) {

    document.write('this time u choose it bigger. Try smaller one');

  } else if (parseInt(an_Guess) < randomNumber) {

    document.write('this time u choose it smaller. Try bigger one');

  }
} else if (parseInt(Guess) < randomNumber) {

  document.write('this time u choose it smaller. Try bigger one');
}

// could you plz check this code also.

Could you please elaborate further on if else if loop,,, why else if not else.

only if and else if statements can have conditions. the else is a "catch all" to anything other than the conditions you specify.

okz got it, thanx bro