【问题标题】:Switch statements in Javascript return the default every time, but not the cases [duplicate]Javascript中的switch语句每次都返回默认值,但不是情况[重复]
【发布时间】:2020-06-17 08:30:47
【问题描述】:

我试图从Mozilla Foundation website 解决问题条件 3。问题是,活动要求在 if(machineActive) 中仅使用 Switch 语句。我使用 if-else 解决了它,但是一旦我尝试使用 switch 语句,控制台就会显示我设置的默认消息,“肯定有问题”,无论 score 变量获得什么值。每当我更改案例时,只会显示默认消息。

在这种情况下使用 switch 语句的正确方法是什么? if-else 是最合适的选择,但这次我想坚持任务的规则。

let response;
let score = 75;
let machineActive = true;

if(machineActive) {


    switch(score){

        case (score<=100 && score>=90):
            response = "What an amazing score! Did you cheat? Are you for real?";
        break;

        case (score<=89 && score>=70):
            response = "That\'s a great score, you really know your stuff.";
        break;


        case (score<=69 && score>=40):
            response = "You did a passable job, not bad!";
        break;


        case (score<=39 && score>=20):
            response = "You know some things, but it's a pretty bad score. Needs improvement.";
        break;

        case (score<=19 && score>=0):
            response = "That was a terrible score — total fail!";
        break;

        default:
            response = "Something must be wrong";

    }


} else {
  response = 'The machine is turned off. Turn it on to process your score.';
}




【问题讨论】:

    标签: javascript if-statement switch-statement


    【解决方案1】:

    switch 语句用于确定是否有任何 case 等于传入的变量。您传入的是一个数字,因此您的 boolean case 都不会等于该数字。您将要传入 true 并查看是否有任何案例评估为 true。

    用于匹配表达式的 case 子句。如果表达式与指定的 valueN 匹配,则 case 子句中的语句将被执行,直到 switch 语句结束或中断。来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

    此外,案例是按顺序评估的,由于您有休息时间,因此您无需检查每个案例的范围上限

    switch (true) {
      case score > 100: 
        response = "You definitely cheated. You must have gone to https://www.stackoverflow.com to get answers to all these questions."
        break
    
      case score >= 90:
        response = "What an amazing score! Did you cheat? Are you for real?"
        break
    
      case score >= 70:
        response = "That's a great score, you really know your stuff."
        break
    
      case score >= 40:
        response = "You did a passable job, not bad!"
        break
    
      case score >= 20:
        response =
          "You know some things, but it's a pretty bad score. Needs improvement."
        break
    
      case score >= 0:
        response = "That was a terrible score — total fail!"
        break
    
      default:
        response = "Something must be wrong"
    }
    

    【讨论】:

    • "switch 语句用于确定是否有任何情况等于传入的变量"
    【解决方案2】:

    你需要

    switch (true) {
    

    因为您正在检查布尔值。

    通过使用score,唯一带有

    的部分
    case score:
    

    会起作用,因为switch 使用严格的比较。

    然后你可以省略所有的 parenteses 并通过使用 filte 对上面的 off 值进行分层,然后使用较小的值,最后使用负值的默认值来使用 fall through。

    let response;
    let score = 75;
    let machineActive = true;
    
    if (machineActive) {
        switch (true) {
            case score > 100:
                response = "Something must be wrong";
                break;
            case score >= 90:
                response = "What an amazing score! Did you cheat? Are you for real?";
                break;
            case score >= 70:
                response = "That\'s a great score, you really know your stuff.";
                break;
            case score >= 40:
                response = "You did a passable job, not bad!";
                break;
            case score >= 20:
                response = "You know some things, but it's a pretty bad score. Needs improvement.";
                break;
            case score >= 0:
                response = "That was a terrible score — total fail!";
                break;
            default:
                response = "Something must be wrong";
        }
    } else {
        response = 'The machine is turned off. Turn it on to process your score.';
    }
    
    console.log(response);

    【讨论】:

    • 这与重复问题的答案有何不同?
    • 它只需要一次检查,并使用从大到小的条件的顺序。其他顺序也是可以考虑的。
    【解决方案3】:

    将switch(score){ 更改为switch(true){

    它试图将 (score&lt;=100 &amp;&amp; score&gt;=90) === score 等同于它没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多