I need to implement a while loop if no input is there then a
I need to implement a while loop, if no input is there then ask again for the name. Also, valid input for the age 0 and 150 if not keep asking. This is what I have right now. JAVASCRIPT
Solution
Hi,
I have added the validations and highlighted the code changes below.
var name = prompt(\"Please type your name:\");
while (name.trim() == \"\" || name == \'undefined\'){
alert(\"You did not type your name. Type it again.\");
name = prompt(\"Please type your name:\");
}
var age = prompt(\"Please type your age:\");
while (age == \'undefined\' || age.trim() == \"\" || (age < 0 || age > 150)){
alert(\"Valid ages are 0 - 150 inclusive\");
age = prompt(\"Please type your age:\");
}
alert(\"Hello \"+name+ \". \" + \"You are \" +age+ \" years old!\");
}
