Could someone give me a hint what is wrong here?
var age = 18;
prompt('please add your age...');
if (age > 18) {
alert("Welcome.");
}
if (age < 18) {
alert("You are not allowed..");
}
You aren't assigning the input to the age variable, but you are setting it to 18, which is not covered by your if or else. Few things you need to do:
Please assign the variable to the user given value.
Check for equality too. What if the user gives 18?
Also, using else if would be a better candidate here.
Your final code should be:
// Change below.
var age = prompt('please add your age...');
// Change condition below.
if (age >= 18) {
alert("Welcome.");
} else if (age < 18) { // Use else if.
alert("You are not allowed..");
}
// var age = 18
// prompt('please add your age...') // this prompts but isnt doing anything with the returned value. try
var age = prompt('please add your age...')
if (age > 18) {
alert('Welcome.')
}
if (age < 18) {
alert('You are not allowed..')
}
if (age === 18) {
alert('this is true')
}
Neither of your conditions were true. Age WAS 18, but you were checking if age was above of below 18.
Also if you wanted to assign the user input value to age, the prompt function returns that value into the age variable like above.
Use a variable for your prompt input.
const age = 18;
const value = prompt('please add your age...');
if (age > value) {
alert("Welcome.");
}
else if (age < value) {
alert("You are not allowed..");
}
const age=prompt('please add your age...');
if (age > 18) alert("Welcome.");
else alert("You are not allowed.");
Related
let age = Number(prompt('Enter your age'))
if (age <= 0) {
alert('Enter a valid age')
console.error('Enter a valid age');
}
if (age < 18) {
alert('You cannot drive')
}
else if (age >= 18) {
alert('Yes you can drive')
}
let askAgain = confirm('Do you want to see thr prompt again?')
while (askAgain == true) {
let age = Number(prompt('Enter your age'))
if (age <= 0) {
alert('Enter a valid age')
console.error('Enter a valid age');
}
else if (age >= 18) {
alert('Yes you can drive')
}
if (age < 18) {
alert('You cannot drive')
}
let askAgain = confirm('Do you want to see thr prompt again?')
}
I want to make the confirm box go away if the user selects cancel. It works fine the first time but if I select ok first and the while loop executes and then It keeps on showing the confirm box even if I select cancel.
You have declared askAgain variable twice - first outside the while loop and then again inside the loop.
Just remove the 'let' keyword present in front of askAgain (which is present inside of while loop). This will fix the issue.
How would you make an if/else statement loop back to the beginning to get user information?
This is the code I got so far:
var age = prompt("Please enter your age");
if(age == 21 ) {
alert("Happy 21st Birthday!");
} else if (age > 21 ) {
alert("You are old");
} else {
alert("Please enter an age");
}
I'm trying to make it go back to the beginning to make the user enter information.
var age = '';
while(age == '' || age == 'ok'){
age = prompt("Please enter your age");
if($.isNumeric(age) === false){
continue;
}
if(age == 21 ){
alert("Happy 21st Birthday!");
continue;
}
if (age > 21 ){
alert("You are old");
continue;
}
if (age < 21){
alert("You are too young to be in this bar!");
}
}
for (let age = prompt('Please enter your age'); ;) {
if (age == 21) {
alert('Happy 21st Birthday!');
break;
} else if (age > 21) {
alert('You are old');
break;
} else {
age = prompt('Please enter your age');
}
}
You separate validation logic from the user input logic.
If this is console app then you would place a loop around the prompt and then validate the user age, if the age is valid break out of the loop otherwise let the loop continue.
On a web page you would wrap it in a function and based on the result manipulate the view based on if the age is correct or not. So you would perhaps show an error message if the age is invalid or go to the next page if the age is valid.
You should wrap the if statements which make up the validation logic in into a function perhaps validateAge that returns true or false, that way no matter what you implement you can use the same method.
Working off of #wallyk's suggestion using a while loop yields this example:
var age = false;
while (!age) {
age = prompt("Please enter your age");
if (age == 21) {
alert("Happy 21st Birthday!");
} else if (age > 21) {
alert("You are old");
} else if (!!age && age < 21) {
alert("You are young");
} else {
alert("Please enter an age");
age = false;
}
}
It will keep looping until you type a valid number answer. I also added in a check to see if the user input an age under 21, since I'm guessing you don't want to keep looping forever if the user is under 21 (or keep looping until they turn 21) but that part can easily be removed if you want.
Hi what am I doing wrong with this if statement? Ive tried making the second one and else if and the last one an else as well but cant get the alerts to respond properly.
prompt("Please enter a number");
if(x < 100) {
alert("variable is less 100")
}
if(x == 100) {
alert("variable is equal to 100!")
}
if(x > 100) {
alert("variable was greater than 100")
}
thanks!
You are missing an assignment to variable x.
var x = prompt("Please enter a number");
//^^^^^
Then you could use parseInt to get a integer number from the string
x = parseInt(x, 10);
const HIGH_AGE = 70, MAX_AGE = 120, BABIE_AGE=4, TEEN_AGE=16;
function main ()
{
var age;
age = Number(prompt('Enter your age:')); //set prompt box ask user's age
if (age < BABIE_AGE || age > MAX_AGE)//if user user's age are below "0" or above "120"
alert('Please Enter Age Within the Range between 0-120!');//output: must enter age in valid age range
else if (isNaN(age) == true || !age)
alert('please enter a valid entry');
else if ((age >= HIGH_AGE) || (age >= 0 && age <= BABIE_AGE))//if age between 0-4 or above 70
alert('You may travel for free');//output: they can travel for free
else if (age >= CHILDREN_AGE && age <= TEEN_AGE )//if user's age are under 16
alert('You may travel with a half price ticket');//output they can travel for half-price
else
alert('You must travel with a full price ticket'); //others all travel with full price
}
Use isNaN function:
if (isNaN(age)) {
// ... age is not valid number
}
Number(prompt('...')) will return NaN value if it can't interpret input as numeric.
Just check if it is a number:
age = prompt('Enter your age:');
if (age == +age) alert("Please insert a number!");
else {
// do what you want
}
I just edited your function. You can try it.
const HIGH_AGE = 70, MAX_AGE = 120, BABIE_AGE=0, CHILDREN_AGE=4, TEEN_AGE=16;
function main () {
var age;
while(/^(?:[1-9]+(?:[0-9]+)?|0)$/.test((age = prompt('Enter your age:'))) == false) {
alert("You must enter number!");
}
age = Number(age);
if (age < BABIE_AGE || age > MAX_AGE)//if user's age are below "0" or above "120"
alert('Please enter age within the range between 0-120!');//output: must enter age in valid age range
else if ((age >= HIGH_AGE) || (age >= BABIE_AGE && age <= CHILDREN_AGE))//if age between 0-4 or above 70
alert('You may travel for free');//output: they can travel for free
else if (age >= CHILDREN_AGE && age <= TEEN_AGE )//if user's age are under 16
alert('You may travel with a half price ticket');//output they can travel for half-price
else
alert('You must travel with a full price ticket'); //others all travel with full price
}
Look at this code:
while(/^(?:[1-9]+(?:[0-9]+)?|0)$/.test((age = prompt('Enter your age:'))) == false) {
alert("You must enter number!");
}
Here is working jsfiddle:
http://jsfiddle.net/zono/varrwx32/14/
A quick and dirty solution (the only one i can think of this quickly), is to use parseInt();
I hope this helps.
I'm trying to direct users to different content based on their age. I'm trying to direct people older than 35 to .if_one, 24-34 to if_two and 18-24 to if_three.. what do I add to check if they are between 18-24 or 25-34?
Here is what I have so far:
$("#age").blur(function() {
$('.a1, .a2').hide();
var age = parseInt($('#age').val());
if (age > 35)
{
$('.if_one').show();
$('#a1').text(age);
alert($('#a1').val());
}
else
{
$('.if_two').show();
$('#a1').text(age);
}
});
You use an if statement with many clauses, and you can use the fact they will be tested in a specific order to your benefit. If you get the second clause, you can assume the first did not trigger, and therefore must not be true.
if (age > 35) {
// triggers if age is over 35
} else if (age > 24) {
// age is not over 35, so triggers if between 24 and 35
} else if (age > 18) {
// age is not over 24, so triggers if between 18 and 24
} else {
// age is not over 18, so triggers if younger than 18
}
You mean like this?
if (age > 35) {
//...
} else if (age >= 24 && age <= 35) {
//...
} else if (age >= 18 && age <= 23) {
//...
} else {
//age is 17 or less, don't know if you want to do something here...
}