2

I am very new to JavaScript, and trying to learn using lynda.com.

I'm currently playing around with the if statement.

The code in question:

var value = prompt("1 or 2?");

if ( value == 1 ) {
    alert("You have chosen 1.");
} 

if ( value == 2 ) {
    alert("You have chosen 2.");
}   

if ( value != (1 || 2) ) {
    alert("Invalid number. Try again.");
}

This is what i want to happen: If the number is NOT 1 or 2, i want JS to execute this piece of code:

 if ( value != (1 || 2) ) {
    alert("Invalid number. Try again.");
}

Apparently this is not the correct way to write it out, and i've tried writing it a bunch of different ways. Can someone show me the correct way, possibly using the else statement?

1
  • Though I don't use CoffeeScript myself, you can do something like this: if value not in [1, 2] then if you ever decide to venture into the preprocessor route some day. Commented Aug 15, 2013 at 13:18

11 Answers 11

4
if ( value != 1 && value != 2) {
    alert("Invalid number. Try again.");
}

If you write value != (1||2), the (1||2) will get first evaluated to 1 and then compared to value, so you effectively wrote:

if ( value != 1 ) { [...] }.

This is because the () have a higher predence than !=. You can also read this explanation about operator predence which gives more details.

Sign up to request clarification or add additional context in comments.

4 Comments

I believe the OP effectively wrote if(value != 1)
No. It would get evaluated to 1.
Press F12 and enter (1||2) - it will return 1
Yes, @TobSpr - do that and you'll see the result is 1
3

1

if ( value != 1 && value !=2 ) {
    alert("Invalid number. Try again.");
}

2

 if ( !(value == 1 || value ==2) ) {
    alert("Invalid number. Try again.");
}

3

if ( value == 1 ) {
    alert("You have chosen 1.");
} 
else if ( value == 2 ) {
    alert("You have chosen 2.");
}   
else {
    alert("Invalid number. Try again.");
}

Comments

2

The best way to write this statement would be as follow:

if ( value == 1 )
{
  alert("1");
}
else if ( value == 2 )
{
  alert("2");
}
else
{
  alert("no 1 or 2");
}

The if statement that you are messing on is (1 || 2) What will happen is it will do a Boolean test and return 1. it should look like this

if ( value !== 1 && value !== 2 )
{
  alert("no 1 or 2");
}

Thanks

Comments

2

As already noted, the best is to separate into two NOT statements,and evaluate both:

if ( value != 1 && value != 2) ) {
    alert("Invalid number. Try again.");
}

However, you could also use the if, else if, else pattern to cover yourself against all other inputs (letters, punctuation, whitespace, etc). The else acts as a catch-all at the end:

var value = prompt("1 or 2?");

if ( value == 1 ) {
    alert("You have chosen 1.");
} 

else if ( value == 2 ) {
    alert("You have chosen 2.");
}   

else {
    alert("Invalid number. Try again.");
}

Comments

1

Inline statements like: (1 || 2) only evaluate the right hand side of the || if the left hand side is false-y. So what your statement actually is saying is:

if ( value != 1 )

Because 1 evaluates to true.

As most others have pointed out, what you actually want to do is:

if ( value != 1 && value != 2 )

Comments

1

The logic is incorrect. You have to do in this way

if ( value != 1 && value != 2) ) {
    alert("Invalid number. Try again.");
}

the (1||2) statement is evaluated as 1 so you are testing if value != 1

Comments

1

have tried writing

if(value != 1 && value != 2)

4 Comments

Tell me a value that doesn't pass this condition.
the value would be 1 and the condition is not meet
@Saint: He's changed || to && now.
did not notice... sorry
0

You have to do it like

if ( value != 1 && value != 2 ) {
    alert("Invalid number. Try again.");
}

OR

if (!(value == 1 || value == 2)) {
    alert("Invalid number. Try again.");
}

Comments

0

You can use switch as follows

switch(value) {
  case 1:
    alert("You have chosen 1.");
    break;
  case 2:
    alert("You have chosen 2.");
    break;
  default:
    alert("Invalid number. Try again.");
}

Comments

0

Your problem is here:

if ( value != (1 || 2) )

What ths is doing is checking if "value" is not equal to (1 OR 2), where (1 OR 2) is evaluated first, because it's in the brackets. (1 OR 2) evaluates to 1, since 1 is truthy in javascript. What your code is actually doing is checking if your value is not equal to 1.

What you actually want to do is check if your value is !=1 and that your value is !=2. You do that like this:

if (value !=1 && value !=2) { /* do something */ }

1 Comment

No problem, just fixed your SyntaxError: Unexpected end of input
-1

You don't need a clause. You can just use:

else{
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.