【问题标题】:JS NaN comparison in switch cases切换案例中的 JS NaN 比较
【发布时间】:2016-10-29 15:52:07
【问题描述】:

制作一个函数,但无法理解如何在 switch case 中进行比较,因此 NaN === case 为真并返回“输入数字为 Number.NaN”;

 function whatNumberIsIt(n){
      var str;
      switch (n) {
      case Number.MAX_VALUE : str = "Input number is Number.MAX_VALUE";
      break;
      case Number.MIN_VALUE : str = "Input number is Number.MIN_VALUE";
      break;
      case Number.NaN : str = "Input number is Number.NaN";
      break;
      case -Infinity : str = "Input number is Number.NEGATIVE_INFINITY";
      break;
      case Infinity : str = "Input number is Number.POSITIVE_INFINITY";
      break;
      default : str = "Input number is " + n;
      }
      return str;
    }
whatNumberIsIt(NaN)

【问题讨论】:

  • 您可以在默认情况下插入支票。使用isNaN(),如果它是NaN,您可以显示不同的字符串。
  • @Teemu 谢谢,也试过这个,但还是不行(@MohitBhardwaj 在这种情况下,除了 max_value、min_value、infinity 和 nan 等其他情况,我需要我的默认情况。跨度>
  • @Teemu 你可以运行一个函数,你会看到在你的情况下(case !+n : str = 'Input number is NaN'; break;)它不会去做并返回默认情况。
  • NaN == NaNfalse 所以你不能使用 switch 来检查一个值是否是 NaN
  • @OleksandrBuchek 确实,我的错。删除了 cmets。

标签: javascript types nan


【解决方案1】:

比较switch语句中的isNaN,检查是否为假。代码如下:

  function whatNumberIsIt(n){
        var str;

        /**
         * The  !isNaN(n)  here is really the secret sauce.
         * This means the value has to be a real number before comparisons will 
         * even happen. That's why we're able to compare "false" in the switch... otherwise
         * this code wouldn't work.
         */
        switch (!isNaN(n) && n) {
            case Number.MAX_VALUE : 
                str = "Input number is Number.MAX_VALUE";
                break;

            case Number.MIN_VALUE : 
                str = "Input number is Number.MIN_VALUE";
                break;

            case false : 
                str = "Input number is Number.NaN";
                break;

            case -Infinity : 
                str = "Input number is Number.NEGATIVE_INFINITY";
                break;

            case Infinity : 
                str = "Input number is Number.POSITIVE_INFINITY";
                break;

            default : str = "Input number is " + n;
        }

        return str;
      }

  console.log( whatNumberIsIt("String") );
  console.log( whatNumberIsIt(NaN) );
  console.log( whatNumberIsIt(1) );
  console.log( whatNumberIsIt(Infinity) );
  console.log( whatNumberIsIt(-Infinity) );

小提琴:https://jsfiddle.net/87o83q2q/

这是您应该期待的输出:

Input number is Number.NaN
Input number is Number.NaN
Input number is 1
Input number is Number.POSITIVE_INFINITY
Input number is Number.NEGATIVE_INFINITY

【讨论】:

  • 我真的相信我尝试过假案。有用!谢谢
  • 这不仅仅是关于false,您必须将比较运算符添加到switch语句本身。请参阅我编辑过的关于!isNaN(n) 的注释……这就是它起作用的原因。没有它,它将继续失败。
  • 以前没有注意到,但现在我完全糊涂了。你能解释一下 switch (!isNaN(n) && n) 以及它如何处理错误的情况吗?
  • 因为当你有一行语句时,它们是按顺序计算的。如果失败,它将停止评估。因此,如果您要比较 !isNaN(n) .. 将返回 false 字符串和任何 NaN,因为它是布尔比较。如果它 IS 是一个数字,它将继续通过语句继续到 n 并将其用于切换。
猜你喜欢
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多