Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Is there way to shorten this long if statement code?

I have the code like below to return the number that makes the sum of each line the same.

let getnums = () => {
  var numsarray = [];
  var arrays = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  var len = arrays.length;
  while (len > 0) {
    var rans = Math.floor(Math.random() * len);
    numsarray.push(arrays[rans]);
    arrays.splice(rans, 1);
    len = len - 1;
  }

  return numsarray;
};
let done = false
function test(num){
while (!done) {
  let output = getnums();
  if (
    output[0] + output[1] + output[2] == 15 &&
    output[1] + output[4] + output[7] == 15 &&
    output[2] + output[5] + output[8] == 15 &&
    output[0] + output[3] + output[6] == 15 &&
    output[2] + output[4] + output[6] == 15 &&
    output[3] + output[4] + output[5] == 15 &&
    output[6] + output[7] + output[8] == 15 &&
    output[0] + output[4] + output[8] == 15
  ) {
    done = true
    console.log(output)
  }
}
}
test()

The code works fine, but I am looking for ways to shorten the if statement.

My current code only will work result 3*3 because of the specific if statement , I am looking for a better and that will work for all different sizes:

E.g: 4*4, a function produces:

if(
output[0] + output[1] + output[2] +output[3]
output[4] + output[5] + output[6] +output[7]
....
)

I have already had idea or using loop to make randomnumber, but don't know how to make if statement.

Is it possible I could use loop or other methods to create the if statement?

Any suggestions or ideas?

Thank you!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Since you have perfect square matrices, you can use the below logic:

  • Every row needs to have same sum.
  • Every column needs to have same sum.
  • Both diagonals need to have same sum.

So, we add every element of the matrix twice and add diagonal elements one more time. Needlessly to say that the value at the center of the matrix will be added 4 times. We do get a total sum.

We need to compare this sum with the new desired sum derivation in the following way:

desired_sum * (R + C + 2)

where R is no. of rows, C is no. of columns and 2 is for the 2 diagonals. Since R = C for your case, it will be

 desired_sum * (N * 2 + 2)

where N is the no. of rows or columns of the matrix.

Snippet:

var mat = [
    [1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1]
  ];

function test(mat, N, desired_sum){
  let sum = 0;
  let d1_col = 0,d2_col = N - 1;
  for(let i = 0; i < N; ++i, d1_col++, d2_col--){
    for(let j = 0; j < N; ++j){
      sum += 2 * mat[i][j];
      if(d1_col == j) sum += mat[i][j];
      if(d2_col == j) sum += mat[i][j];
    }
  }
  return sum == desired_sum * (N * 2 + 2);
}

console.log(test(mat, mat.length, 4));

about 4 years ago · Juan Pablo Isaza Report

0

For the 3x3 problem, you could take the following array of indices and check the sum.

For 4x4 arrays, take another array of indices.

const
    getSum = indices => indices.reduce((sum, index) => sum + output[index], 0),
    indices3 = [[0, 1, 2], [1, 4, 7], [2, 5, 8], [0, 3, 6], [2, 4, 6], [3, 4, 5], [6, 7, 8], [0, 4, 8]];

if (indices3.every(indices => getSum(indices) === 15)) {
    // ...
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!