Javascript 函数,否则,isNaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22893516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:58:35  来源:igfitidea点击:

Javascript function, if else, isNaN

javascriptfunctionif-statement

提问by user3503415

I need help to figure out what I'm doing wrong, I've tried to make a function where you're supposed to enter two numbers in two different boxes and loop it until you put in a valid number! (Sorry for my code and my english)

我需要帮助找出我做错了什么,我试图制作一个函数,你应该在两个不同的框中输入两个数字并循环它直到你输入一个有效的数字!(对不起,我的代码和我的英语)

var x = parseInt(prompt("Please enter a number!"));
var y = parseInt(prompt("Please enter a number!"));

function add(x, y) {

    var z = x + y;
    var i = false;

    do {
        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 + " = ");
            i = true;
        }
        while (i == false);
    }
}

add(x, y);

回答by wvdz

There are a couple of problems with this code:

这段代码有几个问题:

  1. Your while is misplaced.
  2. The parameters x and y don't make sense, because the user needs to input them.
  3. The prompts asking for the numbers are outside of the loop.
  1. 你的时间放错了地方。
  2. 参数 x 和 y 没有意义,因为用户需要输入它们。
  3. 要求输入数字的提示在循环之外。

Here is the fixed code:

这是固定代码:

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();

回答by Amol

The isNaN() is javascript function returns true if the given value is Not-a-Number.

isNaN() is javascript 函数如果给定的值为 Not-a-Number,则返回 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

回答by Manwal

Try this:

试试这个:

$(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);
});


This is complied demo Version is Fiddle checkout.

这是编译的演示版本是小提琴结帐。

Fiddle

小提琴

回答by T.J. Crowder

There are a couple of issues:

有几个问题:

  1. It's syntactically invalid, you've ended up with a freestanding while (i == false);(which would fine, but would never end if iis ever false) and a dangling }under your code. You need to move the whileline beneath the closing }of the do.

  2. If xor yis NaN, your addfunction loops until they change...but no code in that loop ever changes them.

  1. 它的语法无效的,你已经结束了一个独立的while (i == false);(这很好,但如果永远不会结束i是有史以来false)和悬挂}代码下。你需要移动while的收盘线之下}do

  2. 如果xyNaN,您的add函数将循环直到它们发生变化……但该循环中的任何代码都不会更改它们。

I don't know what you want addto do (since just adding numbers doesn't require a function), but if the goal is to keep prompting the user, you have to move the prompts into the loop:

我不知道你想做add什么(因为只是添加数字不需要函数),但如果目标是不断提示用户,你必须将提示移动到循环中:

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();

回答by Mauno V?h?

You can also do it recursively without using do while loop at all, by asking xand yvalues until both is correct. Also, note that I used radix value of 10 for parseInt(string, radix);reason being that docs describes radixas:

您也可以通过询问xy值直到两者都正确为止,在根本不使用 do while 循环的情况下递归地执行此操作。另外,请注意,我使用基数值为 10 的parseInt(string, radix);原因是 docs 描述radix为:

An integer that represents the radix of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.

一个整数,表示上述字符串的基数。始终指定此参数以消除读者混淆并保证可预测的行为。当未指定基数时,不同的实现会产生不同的结果。

See more from docs of parseInt

parseInt 的文档中查看更多信息

The code example:

代码示例:

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();

See my JsFiddle example

请参阅我的JsFiddle 示例