Treedy
Treedy

Reputation: 11

If / else Statement with Prompt

I am super new like day 3 looking and typing code new and I am trying to run this code but it is not working. I have searched and searched but I don't know what I have messed up. Any help would be appreciated.

*note if I take the else away it will run but it does not follow the rule <500 it just says go away to anything you put in.

var budget = prompt('What is your budget?');
if (budget < 500); {
alert('GO AWAY');
}
else  (budget >= 500); {
alert('That will do');

Upvotes: 0

Views: 5100

Answers (5)

fatmi Mohamed
fatmi Mohamed

Reputation: 1

var budget = prompt("What's your budget ?");

if(budget < 500) {
    
    alert("GO AWAY");
}

else {
    alert("that will do");
}

Upvotes: -1

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

Instead of using if...else statements is more simple to use Conditional (ternary) Operator and get a variable message out of your logic.

Than call alert(message) only once:

var budget = prompt('What is your budget?'),
    message = budget < 500 ? 'GO AWAY' : 'That will do';

alert(message);

Note: the previous answers have well pointed all the errors on the OP.

Upvotes: 1

prasanth
prasanth

Reputation: 22490

It's a else if not else .And remove the ; in if and else statement .

Note*

For your case you have one condition is enough .less 500 second else always match 500 and above.if you have more number of condition you could use elseif

var budget = prompt('What is your budget?');
if (budget < 500) {
  alert('GO AWAY');
} else{
  alert('That will do');
}

Upvotes: 2

Arg0n
Arg0n

Reputation: 8423

The second check is redundant. And also, you had ; after if and else, remove those:

var budget = prompt('What is your budget?');
if (budget < 500) {
  alert('GO AWAY');
} else {
  alert('That will do');
}

Upvotes: 1

jdmdevdotnet
jdmdevdotnet

Reputation: 1

A couple errors here. Change what you have to this:

var budget = prompt('What is your budget?');
if (parseInt(budget) < 500) {
alert('GO AWAY');
}
else {
alert('That will do');
}

See fiddle

Basically you had a ; after your if statement and after the parenthesis. This is not needed. Also, else does not accept parameters.

Upvotes: 0

Related Questions