gpt4 book ai didi

javascript - 简短的 Javascript 程序问题(错误输出)

转载 作者:行者123 更新时间:2023-11-30 17:14:46 25 4
gpt4 key购买 nike

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
}

最佳答案

使用isNaN功能:

if (isNaN(age)) {
// ... age is not valid number
}

Number(prompt('...')) 如果无法将输入解释为数字,将返回 NaN 值。

关于javascript - 简短的 Javascript 程序问题(错误输出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26329019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com