为两个输入变量简化我的 isNaN 函数 - JavaScript
Simplify my isNaN function for two input variables - JavaScript
我有两个变量,我正在通过两个提示获得。一个是 startNum,另一个是 rangeNum。我试图错误捕获它,以便如果它们不是正数,它将重新提示用户,直到给出正值。目前我通过 运行 isnan 为每个 vaiable 工作,但我想知道我是否可以只有一个函数来检查两个提示输入的数字是否为正数,如果不是,它会重新提示正确的提示给用户。提前感谢任何 help/answers.
function isPosNum() {
startNum = parseInt(prompt("Please enter a positive starting value"));
if (isNaN(startNum)) {
return NaN;
} else if (startNum < 0) {
console.log('failed');
alert('That is not a positive number. Please try again and enter a positive number.');
return "negative";
isPosNum();
} else if ( startNum > 0) {
console.log('worked');
//push initial value to the array
numArray.push(startNum);
//enterRange();
return "positive";
} else {
return "zero";
}
function enterRange() {
rangeNum = parseInt(prompt("Please enter a number to determine the range of values."));
if (isNaN(rangeNum)) {
return NaN;
} else if (rangeNum < 0) {
console.log('failed');
alert('That is not a positive number. Please try again and enter a positive number.');
return "negative";
enterRange();
} else if ( rangeNum > 0) {
console.log('worked');
//push initial value to the array
//collatz();
return "positive";
} else {
return "zero";
}
}
}
由于 deamentiaemundi 的回答,我能够让代码正常工作。这是我给那些希望看到的人的最终代码。
function getStartNum(){
startNum = parseInt(prompt('Please enter a starting number greater than 0.'));
if(!isPosNum(startNum)){
alert("error! That is an incorrect value. Please renter an appropriate positive value.");
getStartNum();
} else {
getRangeNum();
}
}
function getRangeNum(){
rangeNum = parseInt(prompt('Please enter a range value greater than 0'));
if(!isPosNum(rangeNum)){
alert("error! That is an incorrect value. Please renter an appropriate positive value.");
getRangeNum();
}
// and so on
}
function isPosNum( number ) {
if (isNaN( number )) {
return false;
} else if (number < 0) {
return false;
} else if (number == 0) {
return false;
} else {
return true;
}
}
创建一个函数 checkRange(number)
,它将数字作为参数,returns 根据该数字选择 true
或 false
。
第二个函数是有提示的函数
function getNumbers(){
startNum = parseInt(prompt('Please enter a number'));
if(!checkRange(startNum)){
alert("error!");
getNumbers();
}s
rangeNum = parseInt(prompt('Please enter a range'));
if(!checkRange(rangeNum)){
alert("error!");
getNumbers();
}
// and so on
}
我有两个变量,我正在通过两个提示获得。一个是 startNum,另一个是 rangeNum。我试图错误捕获它,以便如果它们不是正数,它将重新提示用户,直到给出正值。目前我通过 运行 isnan 为每个 vaiable 工作,但我想知道我是否可以只有一个函数来检查两个提示输入的数字是否为正数,如果不是,它会重新提示正确的提示给用户。提前感谢任何 help/answers.
function isPosNum() {
startNum = parseInt(prompt("Please enter a positive starting value"));
if (isNaN(startNum)) {
return NaN;
} else if (startNum < 0) {
console.log('failed');
alert('That is not a positive number. Please try again and enter a positive number.');
return "negative";
isPosNum();
} else if ( startNum > 0) {
console.log('worked');
//push initial value to the array
numArray.push(startNum);
//enterRange();
return "positive";
} else {
return "zero";
}
function enterRange() {
rangeNum = parseInt(prompt("Please enter a number to determine the range of values."));
if (isNaN(rangeNum)) {
return NaN;
} else if (rangeNum < 0) {
console.log('failed');
alert('That is not a positive number. Please try again and enter a positive number.');
return "negative";
enterRange();
} else if ( rangeNum > 0) {
console.log('worked');
//push initial value to the array
//collatz();
return "positive";
} else {
return "zero";
}
}
}
由于 deamentiaemundi 的回答,我能够让代码正常工作。这是我给那些希望看到的人的最终代码。
function getStartNum(){
startNum = parseInt(prompt('Please enter a starting number greater than 0.'));
if(!isPosNum(startNum)){
alert("error! That is an incorrect value. Please renter an appropriate positive value.");
getStartNum();
} else {
getRangeNum();
}
}
function getRangeNum(){
rangeNum = parseInt(prompt('Please enter a range value greater than 0'));
if(!isPosNum(rangeNum)){
alert("error! That is an incorrect value. Please renter an appropriate positive value.");
getRangeNum();
}
// and so on
}
function isPosNum( number ) {
if (isNaN( number )) {
return false;
} else if (number < 0) {
return false;
} else if (number == 0) {
return false;
} else {
return true;
}
}
创建一个函数 checkRange(number)
,它将数字作为参数,returns 根据该数字选择 true
或 false
。
第二个函数是有提示的函数
function getNumbers(){
startNum = parseInt(prompt('Please enter a number'));
if(!checkRange(startNum)){
alert("error!");
getNumbers();
}s
rangeNum = parseInt(prompt('Please enter a range'));
if(!checkRange(rangeNum)){
alert("error!");
getNumbers();
}
// and so on
}