简体   繁体   English

与if / else语句一起使用

[英]function with if/else statement

I am creating a function that should add the sum of three cards for both the dealer and the player. 我正在创建一个功能,该功能应为发牌人和玩家增加三张牌的总和。 The values should then be evaluated and what is given back from the function should be a recommendation, "safe", "stop", "black jack" or "busted". 然后应该评估这些值,并且从功能返回的值应该是“安全”,“停止”,“黑杰克”或“无效”的建议。 There will be two different functions evaluating the player´s cards and the dealer´s. 将有两种不同的功能可评估玩家的纸牌和庄家的纸牌。 Please look at the code I have written. 请查看我编写的代码。 I think I have figured it out codewise, I just have one question. 我想我已经在代码上找到了答案,我只是有一个问题。

function printResult(p1, p2, p3, d1, d2, d3) {
  var x = p1 + p2 + p3; 
  var ab = "player:" + " " + player(x);
  var y = d1 + d2 + d3;
  var de = "dealer:" + " " + dealer(y);
  return ab + ", " + de;
 };

function player(p1, p2, p3) {
  var summa = p1 + p2 + p3
  var result;
  if (summa < 21)
  result = "safe";
  else if (summa = 21)
    result = "black jack";
  else
    result = "busted";

  return result;
}

function dealer(d1, d2, d3) {
  var summa = d1 + d2 + d3;
  var dealer;
  if (summa < 17)
  dealer =  "safe";
else if (summa >= 17 && summa <= 20)
  dealer = "stop";
else if (summa == 21)
  dealer = "black jack";
else 
  dealer = "busted";

return dealer; 
}

When I print printResult(4, 8, 1, 3, 8, 4) it gives me a faulty recommendation (the player gets busted when it should get safe). 当我打印printResult(4、8、1、3、8、4)时,它给了我一个错误的建议(播放器在应确保安全的状态下会崩溃)。

However, if I change the code to the following, then it works. 但是,如果我将代码更改为以下代码,则它可以工作。

function printResult(p1, p2, p3, d1, d2, d3) {
  var x = p1 + p2 + p3; 
  var ab = "player:" + " " + player(p1, p2, p3);
  var y = d1 + d2 + d3;
  var de = "dealer:" + " " + dealer(d1, d2, d3);
  return ab + ", " + de;
 };

I don´t understand why there is a difference in the result? 我不明白为什么结果会有所不同? Ps, in the latter piece of code I understand that I do not have to have x and y as variables, they can be removed. ps,在后面的代码中,我了解到我不必将x和y作为变量,可以将它们删除。

Thanks in advance! 提前致谢!

There is a difference: 它们是有区别的:

var x = p1 + p2 + p3; 
var ab = "player: " + player(x);

You're calling player with one parameter, x , that contains the sum of your variables. 您正在用一个参数x调用player ,该参数包含变量的总和。 This is valid code, but the other parameters will now be undefined . 这是有效的代码,但是其他参数现在是undefined To illustrate, your function with actually look something like: 为了说明这一点,您的函数实际上如下所示:

function player(p1, p2, p3) {
  var summa = p1 + p2 + p3 //p1 is x, p2 is undefined, p3 is undefined
  ...
  else
    result = "busted";
  return result;
}

Lets say x = 2 . 假设x = 2 When you try to add 2 + undefined + undefined JavaScript will return NaN (Not A Number) and this is what the variable summa will be set to. 当您尝试添加2 + undefined + undefined JavaScript将返回NaN (非数字),这就是变量summa设置为的值。 The only way out from that function is now through "busted" since the other conditions of your if-else are not met (only the else). 现在,退出该功能的唯一方法是通过“无效”,因为不满足if-else的其他条件(仅满足else条件)。

This can be solved by: 这可以通过以下方法解决:

var summa = (p1 + p2 + p3) || (p1 + p2) || p1;

This basically means that you only add the parameters that were passed to the function, ie that exists. 这基本上意味着您只需添加传递给函数的参数,即存在的参数。

Javascript allow you to not specify d2 & d3 parameters to your player function but if they aren't there they would have the value undefined and x + undefined + undefined is NaN (Special value for Not a Number) Javascript允许您不为player函数指定d2d3参数,但如果不存在,则它们的值将为undefined并且x + undefined + undefinedNaN (非数字的特殊值)

If you want to allow both ways to call your function you can add a check at the top that convert undefined to 0 : 如果要允许两种方式调用函数,则可以在顶部添加一个将undefined转换为0的检查:

function player(p1, p2, p3) {
  p1 = p1 || 0;
  p2 = p2 || 0;
  p3 = p3 || 0;
  var summa = p1 + p2 + p3
  var result;
  if (summa < 21)
  result = "safe";
  else if (summa = 21)
    result = "black jack";
  else
    result = "busted";

  return result;
}

This syntax works because undefined is 'falsey' so the value of 0 will be used, 0 is also 'falsey' but it's not important because it will be replaced by 0 and all other values will be kept as they are 'truthy' 此语法有效,因为undefined为'falsey',因此将使用0值,0也为'falsey',但这并不重要,因为它将被0替换,并且所有其他值将保留为“ true”。

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

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