简体   繁体   English

Javascript - 为什么我的功能出现NAN错误

[英]Javascript - Why am I Getting NAN error with my function

<script type="text/javascript">
  var sheep = prompt("How many sheeps do we have in the park?");
  var rangers = prompt("How many rangers do we have in the park?");

  function feedPerRanger (sheep, rangers) {
     alert("Each Park Ranger should load " + sheep*2/rangers + 
           " lb(s) of feed into his/her BART today.");
  }

  feedPerRanger ();
</script>

Hello 你好

I'm still a beginner so this is probably a rookie mistake. 我还是初学者,所以这可能是一个新手的错误。 The script is suppose to alert the user as to how much feed each park ranger is suppose to load on to his/her BART. 该脚本用于提醒用户每个公园护林员想要加载到他/她的BART上的饲料量。 It's suppose to be 2 lbs per every sheep divided by the number of rangers. 假设每只绵羊2磅,除以游侠数量。 I keep getting the following NAN error. 我一直收到以下NAN错误。 Can anyone explain why? 有谁能解释为什么?

"Each Park Ranger should load NaN lb(s) of feed into his/her BART today." “每个公园护林员今天应该将NaN lb(s)的饲料加载到他/她的BART中。”

Change this: 改变这个:

function feedPerRanger (sheep, rangers) {
    alert("Each Park Ranger should load " + sheep*2/rangers + " lb(s) of feed into his/her BART today.");
}

with this: 有了这个:

function feedPerRanger () {
    alert("Each Park Ranger should load " + sheep*2/rangers + " lb(s) of feed into his/her BART today.");
}

Now it works; 现在它有效; but (in my opinion) it is not the better way to do what you want. 但是(在我看来)这不是做你想做的更好的方法。

You may use your function properly, passing two generic parameter as following: 您可以正确使用您的函数,传递两个通用参数如下:

function feedPerRanger(sheep, rangers) {
    alert("Each Park Ranger should load " + sheep*2/rangers + " lb(s) of feed into his/her BART today.");
}

var sheep = prompt("How many sheeps do we have in the park?");
var rangers = prompt("How many rangers do we have in the park?");

feedPerRanger(sheep, rangers);

Edit - as Ian has suggested change the names of the parameters. 编辑 - 正如Ian 建议更改参数的名称。

You should pass the sheep and rangers variables to the function. 您应该将sheeprangers变量传递给函数。 And you should also check if they contain numbers. 你还应该检查它们是否包含数字。 Consider this: 考虑一下:

var sheep = prompt("How many sheeps do we have in the park?");
// sheep = "some random string";
// alert ( sheep / 2 );

I can press "h" or write whatever I want. 我可以按“h”或写任何我想要的东西。 Look at prompt from MDN 从MDN看一下prompt

result (of prompt) is a string containing the text entered by the user, or the value null. result(of prompt)是一个包含用户输入的文本的字符串,或者值为null。

So add a little check if the variable is not a number with isNaN like this: 所以添加一点检查变量是不是isNaN的数字,如下所示:

isNaN(sheep)

Also I can press Cancel in the prompt , then sheep or rangers will be null . 此外,我可以在提示中取消 ,然后绵羊或游侠将​​为null If you change the function like this, there will be no chance to have another NaN : 如果你改变这样的功能,将没有机会再有另一个NaN

function feedPerRanger(sheep, rangers) {
    if ((sheep && !isNaN(sheep)) && (rangers && !isNaN(rangers))) {
        alert("Each Park Ranger should load " + sheep * 2 / rangers +
            " lb(s) of feed into his/her BART today.");
    } else {
        alert("please use numbers");
    }
}

var sheepCount = prompt("How many sheeps do we have in the park?");
var rangersCount = prompt("How many rangers do we have in the park?");

feedPerRanger(sheepCount, rangersCount);

Fiddle 小提琴

Your function definition accepts two parameters. 您的函数定义接受两个参数。

function feedPerRanger (sheep, rangers)

You give none in your call: 你没有给你打电话:

feedPerRanger ();

(Additionally, there's no space between the name of the function and the parentheses) (此外,函数名称和括号之间没有空格)

When you create your function definition, you're giving names to local variables. 在创建函数定义时,您将为局部变量指定名称。 That is, sheep and ranger are the names given to the parameters passed so that they can be used in the function. 也就是说, sheepranger是传递给参数的名称,因此它们可以在函数中使用。 So, when you create the function definition, sheep and ranger are not the variables you created above. 因此,当您创建函数定义时, sheepranger不是您在上面创建的变量。

There are two ways to fix this. 有两种方法可以解决这个问题。

  1. Remove your parameters from the function definition: 从函数定义中删除参数:

     function feedPerRanger(){...} 
  2. Pass in your variables as parameters to the function: 将变量作为参数传递给函数:

     function feedPerRanger(numSheep, numRangers){ alert("Each Park Ranger should load " + numSheep*2/numRangers + " lb(s) of feed into his/her BART today."); } feedPerRanger(sheep, rangers) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM