1

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;
    }
7
  • 4
    You seem to be looking for simple arithmetic: answer = [7-max.length, 7-min.length]; Commented Mar 29, 2022 at 1:12
  • 1
    I believe @Bergi has provided the correct answer. But whenever you have a similar situation again where such a solution is not possible at least use a switch statement or map the values using a JS Object. Commented Mar 29, 2022 at 1:17
  • Try this: answer[0] = max.length <= 6 ? 7 - max.length : 6; and answer[1] = min.length <= 6 ? 7 - min.length : 6;. Commented Mar 29, 2022 at 1:24
  • @jsN00b i tired but it some thing wrong Commented Mar 29, 2022 at 1:42
  • 1
    OP is trying to provide examples of what the two array's max and min may look like. Sort-of like test cases, I think. And the result is what is expected. And, case 2 - does seem incorrect to me. Commented Mar 29, 2022 at 1:50

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

The question says "Case 2" has min as [0, 0] - which has length 2. If length is 2, then the answer will be 5. But, the expected result is 6. So, in the above snippet, "Case 2" has min as [0] which is of length 1 and this gets the expected result: [1, 6].
thank you man i understood!!!!!1 thank you for help!
1

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

2 Comments

i use your switch code it works! but second one or third one it doesn't work... what did i wrong?.... and i added some of cases in my question. what can i add in length?
maybe you haven't deleted max = {length: -5}; and min = {length: -99};? those are just example of wrongly (or maybe maliciously) inputted variable
0

you need to write just these two lines instead :)

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

4 Comments

in if loop? or just that code?
@jackvolten no need to loop ,I recognized it from the pattern of your values
It can also be destructured like answer = [7 - max.length, 7 - min.length]
It can also be destructured like answer = [7 - max.length, 7 - min.length]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.