Shubham Saini
Shubham Saini

Reputation: 17

How to print value using prompt with if else statement?

var age = '16';
prompt(enter 'age');
if (age > 17) {
  alert("good to drive");
} else {
  alert("sorry you are not eligible");
}

Upvotes: 0

Views: 431

Answers (2)

Kenzoid
Kenzoid

Reputation: 294

You had a few syntax errors but here's a patched up version:

var age = prompt("Please enter your age"); // Gets input
if (age > 17) {
  alert("Good to drive.");
} else {
  alert("Sorry you are not eligible.");
}

Upvotes: 1

Barmar
Barmar

Reputation: 780909

The first two lines should be:

var age = prompt('enter age');

The whole argument to prompt() has to be in quotes, and you need to assign the result to the variable so you can use it in the if statement.

Upvotes: 0

Related Questions