Javascript函数,如果不是,则为isNaN
我需要帮助来找出我做错了什么,我试着做一个函数,你应该在两个不同的框中输入两个数字,然后循环直到你输入一个有效的数字!(对不起,我的代码和英语)Javascript函数,如果不是,则为isNaN,javascript,function,if-statement,Javascript,Function,If Statement,我需要帮助来找出我做错了什么,我试着做一个函数,你应该在两个不同的框中输入两个数字,然后循环直到你输入一个有效的数字!(对不起,我的代码和英语) 此代码有几个问题: 你的时间放错地方了 参数x和y没有意义,因为用户需要输入它们 询问数字的提示在循环之外 以下是固定代码: function add() { do { var x = parseInt(prompt("Please enter a number!")); var y = parseInt(pr
此代码有几个问题:
function add() {
do {
var x = parseInt(prompt("Please enter a number!"));
var y = parseInt(prompt("Please enter a number!"));
var z = x + y;
var i = false;
if (isNaN(x)) {
alert("Invalid entry. Please enter a number!")
} else if (isNaN(y)) {
alert("Invalid entry. Please enter a number!")
} else {
alert(x + " + " + y + " = " + z);
i = true;
}
}
while (i == false);
}
add();
试试这个:
$(document).ready(function(){
var x = parseInt(prompt("Please enter First number!"));
var y = parseInt(prompt("Please enter Second number!"));
function add(x, y) {
var z = x + y;
var i = false;
do {
if (isNaN(x)) {
alert("Invalid entry for first number. Please enter a number!");
x = parseInt(prompt("Please enter first number again!"));
} else if (isNaN(y)) {
alert("Invalid entry for second one. Please enter a number!");
y = parseInt(prompt("Please enter Second number again!"));
} else {
z=x+y;
alert(x + " + " + y + " = "+ z);
i = true;
}
}while (i == false);
}
add(x, y);
});
这是一个演示版本,是小提琴检查
有几个问题:
while(i==false)代码>(如果i
为false
,则这将很好,但永远不会结束),并且代码下有一个悬空的}
。您需要在do
的结束}
下方移动while
行
x
或y
为NaN
,则您的添加
函数循环,直到它们发生更改…但该循环中的任何代码都不会更改它们function add() {
var x, y, z;
var valid = false;
while (!valid) {
x = parseInt(prompt("Please enter a number!"));
y = parseInt(prompt("Please enter a number!"));
valid = !isNaN(x) && !isNaN(y);
if (!valid) {
alert("Invalid entry. Please enter a number!")
}
}
z = x + y;
// Do something with z
}
add();
您还可以通过询问
x
和y
值,直到这两个值都正确为止,递归地执行此操作,而根本不使用do while循环。另外,请注意,对于parseInt(字符串,基数),我使用了基数值10代码>原因是文档将基数描述为:
表示上述字符串基数的整数。
始终指定此参数以消除读卡器混淆并
保证可预测的行为。不同的实现产生不同的结果
未指定基数时的不同结果
请参阅的文档中的更多信息
代码示例:
function askXY(x, y) {
var x_ = x,
y_ = y;
if(typeof x_ === "undefined") {
x_ = parseInt(prompt("Please enter a number for x!"), 10);
}
if(typeof y_ === "undefined") {
y_ = parseInt(prompt("Please enter a number for y!"), 10);
}
if(isNaN(x_) || isNaN(y_)) {
alert("Invalid entry. Please enter a number!");
// The magic is here, we keep the x or y if either one of those are correct
// and if not, we give undefined so that value will be asked again from the user
return askXY(
!isNaN(x_) ? x_ : undefined,
!isNaN(y_) ? y_ : undefined
);
}
// success!
alert(x_ + " + " + y_ + " = " + (x_ + y_));
}
askXY();
请参见my如果给定值不是数字,则isNaN()is javascript函数将返回true
var a = isNaN('127') ; // return false
var a = isNaN('1273 ') ; // return false
var b = isNaN(-1.23) ; // return false
var c = isNaN(5-2); // return false
var d = isNaN(0) ; // return false
var e = isNaN("Hell o") ; // return true
var f = isNaN("2005/12/12"); // return true
将提示添加到循环中,这样就可以真正更改值,而不仅仅是查看警报了,怎么样?哇,这是一些误导性的缩进。事实上,在语法上无效的代码。在两个地方有提示是一个维护问题。你是正确的@T.J.Crowder我有更新答案,但你的答案也很完美。:)你为什么要用递归来解决这个问题?@T.J.Crowder因为这是可能的,而且在我看来,它看起来很干净,这在一定程度上只是一个品味问题。:)你能描述一下为什么递归不适合这种情况吗?简单循环的递归使用了错误的工具,在这种情况下,代码明显比需要的复杂。你可以用套筒扳手钉钉子,但最好的做法是使用锤子(或射钉枪:-)。我尊重你的回答,但我认为你的论点是基于观点的,这很好。我想提供一种适合我口味的、有效且干净的替代品。编程充满了可供选择的解决方案,这里没有什么新鲜事,aight?:)同意,很多事情都是意见或品味的问题。但我认为对一个简单循环使用递归超越了单纯的观点,而是涉及到复杂化、调试、堆栈消耗等客观问题。无论如何,正如你所说,这是另一种选择。
var a = isNaN('127') ; // return false
var a = isNaN('1273 ') ; // return false
var b = isNaN(-1.23) ; // return false
var c = isNaN(5-2); // return false
var d = isNaN(0) ; // return false
var e = isNaN("Hell o") ; // return true
var f = isNaN("2005/12/12"); // return true