【问题标题】:Is there a more elegant way to write these conditions?有没有更优雅的方式来编写这些条件?
【发布时间】:2019-01-12 20:49:57
【问题描述】:
    if(first - second >=2 || first - second <=-2 || first - third >=2 || first - third <=-2 || second - third >=2 || second - third <=-2)

真恶心。

我有三个值需要检查,如果其中任何两个有 >= 2 的差异,那么我会运行一些任务。

我很好奇,您能提出一种更愉快的方法吗?谢谢

【问题讨论】:

  • 提示:Math.abs.
  • 使用Math.abs可以将查询次数减半
  • 对人类或机器来说更优雅?

标签: javascript loops if-statement conditional


【解决方案1】:
if ( Math.max(first, second, third) - Math.min(first, second, third) >= 2 )

或者效率低一点:

var a = [first, second, third].sort((a, b) => a - b));
if ( a[2] - a[0] >= 2 )

【讨论】:

  • 这篇文章中有一些很好的解决方案,但我认为这已经很优雅了 :) 谢谢 Slai
【解决方案2】:

将减法表达式放入数组中并使用 Array#some() 和 Math.abs

if ( [first - second, first - third , second - third].some(n => Math.abs(n) >=2 ) )

【讨论】:

    【解决方案3】:

    您可以使用 Math.abs() 简单地将 theif 语句分解为 3 个条件。 就是这样:

    if( Math.abs( first - second ) >= 2 || Math.abs( first - third ) >= 2 || Math.abs( second - third ) >= 2 ) 
    

    Math.abs() 函数返回其参数的绝对值。

    【讨论】:

      【解决方案4】:

      如果您愿意使用函数来确定数字数组之间的最大差异,这里有一些解决问题的方法。最短的,如果可能是最简洁的,只是关于如何解决问题的一些想法:

      const shortGetMaxDifference = numbers => Math.max(...numbers) - Math.min(...numbers);
      
      const getMaxDifference = numbers => {
        const sorted = numbers
          .sort((a, b) => a > b ? -1 : 1)
          
        return sorted[0] - sorted[sorted.length-1]
      }
      
      const longWindedGetMaxDifference = numbers => numbers
        .map(i => numbers.map(j => Math.max(i, j) - Math.min(i, j))
        .sort((a, b) => a > b ? -1 : 1))
        .reduce((prev, curr) => [...prev, ...curr], [])
        .sort((a, b) => a > b ? -1 : 1)[0]
        
      const numbers = [1, 4, -5, 7, 13]
      
      console.log(shortGetMaxDifference(numbers))
      console.log(getMaxDifference(numbers))
      console.log(longWindedGetMaxDifference(numbers))
      
      const first = 1;
      const second = 3;
      const third = 2;
      
      if (getMaxDifference([first, second, third]) >= 2) {
        console.log('The difference is bigger');
      }

      【讨论】:

        【解决方案5】:

        Math.abs() 会帮助你,你可以将几个条件分解为只有 3 个条件。

        Math.abs(first - second) &gt;= 2 和类似的其他人

        【讨论】:

          【解决方案6】:

          一种(客观上)更优雅的方法是利用Math.abs() 函数:

          if (Math.abs(first-second) >= 2 || Math.abs(first-third) >= 2 || Math.abs(second-third) >= 2)){
              // do something
          }
          

          一种主观的(我可能会添加完全没有必要)使其在美学上更令人愉悦的方法是:

          function plusminus2(val1, val2){
             return ((Math.abs(val1 - val2) >= 2) ? true : false);
          }
          
          if (plusminus2(first, second) || plusminus2(first, third) || plusminus2(second, third)){
            // do something
          }
          

          但我通常不建议这样做。虽然它可以为您节省几秒钟的时间编写一些 if 语句(假设不止一个)它会引起其他任何试图理解您的代码的人甚至您自己的绝对头痛,如果您4 年后打开它!

          【讨论】:

          • 这里唯一让人头疼的是condition ? true : false这个东西。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多