Home > Net >  How can I reduce duplication in my JavaScript if syntax?
How can I reduce duplication in my JavaScript if syntax?

Time:03-29

I want to reduce duplication in my JavaScript syntax.

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!

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;
    }

CodePudding user response:

is max and min length between 1 and 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

//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

CodePudding user response:

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);

CodePudding user response:

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:

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:

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:

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.

So you should also add some Math.max and Math.min if you want your code to behave exactly like your if-elseif statement:

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

CodePudding user response:

you need to write just these two lines instead :)

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