简体   繁体   English

如何减少 JavaScript if 语法中的重复?

[英]How can I reduce duplication in my JavaScript if syntax?

I want to reduce duplication in my JavaScript syntax.我想减少 JavaScript 语法中的重复。

No matter how much I think about it, it doesn't come to mind.不管怎么想,就是想不起来。

It doesn't matter if it is for loop or any syntax!不管是for循环还是任何语法都无所谓!

i add some of case and result我添加一些案例和结果

if if it is correct answer would be result如果它是正确的答案将是结果

    //   case 1

  //  const max = [1, 31, 0, 0]

  //  const min = [1, 31];
  
  //  result = [3, 5]

  //   case 2

  // const max = [0, 0, 0, 0, 0, 0]

  // const min = [0, 0]

  //  result = [1, 6]

  //   case 3

  // const max = [45, 4, 35, 20, 3, 9]

  // const min = [45, 4, 35, 20, 3, 9]
  
  //  result = [1, 1]

    if (max.length === 6) {
      answer[0] = 1;
    } else if (max.length === 5) {
      answer[0] = 2;
    } else if (max.length === 4) {
      answer[0] = 3;
    } else if (max.length === 3) {
      answer[0] = 4;
    } else if (max.length === 2) {
      answer[0] = 5;
    } else {
      answer[0] = 6;
    }

    if (min.length === 6) {
      answer[1] = 1;
    } else if (min.length === 5) {
      answer[1] = 2;
    } else if (min.length === 4) {
      answer[1] = 3;
    } else if (min.length === 3) {
      answer[1] = 4;
    } else if (min.length === 2) {
      answer[1] = 5;
    } else {
      answer[1] = 6;
    }

is max and min length between 1 and 6?最大和最小长度在 1 到 6 之间吗? because judging your code, it looks like it.因为判断你的代码,它看起来像。

answer[0] = 7 - max.length

sure looks a bit neater, at the least you could eliminate many if blocks and leave the else block in case max.length is not an integer between 1 and 6确实看起来更整洁一点,至少你可以消除许多 if 块并保留 else 块,以防 max.length 不是 1 到 6 之间的 integer

//pseudocode
if( max.length between 1 and 6 inclusive) {
    answer[0] = 7- max.length
} else { 
    answer[0] = some default value, 6? 
}

the 7 looks like a magic number, but with more context, you can name it something better 7 看起来像一个神奇的数字,但是有了更多的上下文,你可以给它起一个更好的名字

By reducing duplication, do you mean you want to decrease the length of your code or increase readability?通过减少重复,你的意思是你想减少代码的长度或增加可读性吗?

If you want to increase readability, some people prefer select/case than if/elseif :如果你想提高可读性,有些人更喜欢select/case而不是if/elseif

switch(max.length) {
  case 6:
    answer[0] = 1;
    break;
  case 5:
    answer[0] = 2;
    break;
  case 4:
    answer[0] = 3;
    break;
  case 3:
    answer[0] = 4;
    break;
  case 2:
    answer[0] = 5;
    break;
  default:
    answer[0] = 6;
}

If you want to reduce length, you can just do something like @Bergi said in comment:如果你想减少长度,你可以像@Bergi 在评论中说的那样做:

answer = [7-max.length, 7-min.length];

But if max and min variable is from user input or from external source, unexpected thing may occurs:但是如果maxmin变量来自用户输入或来自外部源,则可能会发生意想不到的事情:

 max = {length: -5}; min = {length: -99}; answer = [7-max.length, 7-min.length]; console.log(answer) // outputs [12,106]

The code may outputs a number outside 1-6 integer range.该代码可能会输出 1-6 integer 范围之外的数字。

So you should also add some Math.max and Math.min if you want your code to behave exactly like your if-elseif statement:因此,如果您希望代码的行为与 if-elseif 语句完全一样,您还应该添加一些 Math.max 和 Math.min:

 max = {length: -5}; min = {length: -99}; answer = [ Math.max(Math.min(7-max.length,1),6), Math.max(Math.min(7-min.length,1),6) ]; console.log(answer) // outputs [6,6]

Of course if you take input from external source you should sanitize/validate it first, but if it's an overkill, you can also use the above Math.min and Math.max function当然,如果你从外部源获取输入,你应该首先对其进行清理/验证,但如果这是一个矫枉过正的问题,你也可以使用上面的 Math.min 和 Math.max function

 const getAnswers = x => (x < 1 || x > 6)? 6: 7 - x; // case 1 let max = [1, 31, 0, 0] let min = [1, 31]; // result = [3, 5] let answer = [getAnswers(max.length), getAnswers(min.length)]; console.log('max.length: ', max.length, ' min.length: ', min.length, ' answer array: ', answer); // case 2 max = [0, 0, 0, 0, 0, 0] min = [0] // result = [1, 6] answer = [getAnswers(max.length), getAnswers(min.length)]; console.log('max.length: ', max.length, ' min.length: ', min.length, ' answer array: ', answer); // case 3 max = [45, 4, 35, 20, 3, 9] min = [45, 4, 35, 20, 3, 9] // result = [1, 1] answer = [getAnswers(max.length), getAnswers(min.length)]; console.log('max.length: ', max.length, ' min.length: ', min.length, ' answer array: ', answer);

you need to write just these two lines instead:)你只需要写这两行:)

answer[0] = 7 - max.length
answer[1] = 7 - min.length

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

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