Skip to main content
added 279 characters in body
Source Link
tdy
  • 2.3k
  • 1
  • 10
  • 21
function isEqual(a1, a2, a3, a4, a5) {
    if (a1==a2 && a2==a3 && a3==a4 && a4==a5) {
        return true;
    } else {
        return false;
    }
}

With 5 elements, this is already quite verbose, but now imagine checking 20 elements or 1,000. It's better to use Array.every to check all elements for a condition. Here you could check if every element equals the first element.

It's better to test for strict equality (===) to avoid implicit type conversion. Note that 10 == '10' returns true, but 10 === '10' returns false.

Returning true and false is redundant. You can simply return the condition itself.

So the function can be reduced to this:

function isEqual(arr) {
    return arr.every(val => val === arr[0]);
}

And now that the function accepts an array instead of individual values, you can also avoid extracting a1, ..., a5 separately and simply pass an Array.slice:

if (isEqual(record[i].slice(j, j+5))) {
    ...
}

And for the vertical check, use Array.map to get the \$j\$th element of each slice:

if (isEqual(record.slice(i, i+5).map(arr => arr[j]))) {
    ...
}
function isEqual(a1, a2, a3, a4, a5) {
    if (a1==a2 && a2==a3 && a3==a4 && a4==a5) {
        return true;
    } else {
        return false;
    }
}

With 5 elements, this is already quite verbose, but now imagine checking 20 elements or 1,000. It's better to use Array.every to check all elements for a condition. Here you could check if every element equals the first element.

It's better to test for strict equality (===) to avoid implicit type conversion. Note that 10 == '10' returns true, but 10 === '10' returns false.

Returning true and false is redundant. You can simply return the condition itself.

So the function can be reduced to this:

function isEqual(arr) {
    return arr.every(val => val === arr[0]);
}

And now that the function accepts an array instead of individual values, you can also avoid extracting a1, ..., a5 separately and simply pass an Array.slice:

if (isEqual(record[i].slice(j, j+5))) {
    ...
}
function isEqual(a1, a2, a3, a4, a5) {
    if (a1==a2 && a2==a3 && a3==a4 && a4==a5) {
        return true;
    } else {
        return false;
    }
}

With 5 elements, this is already quite verbose, but now imagine checking 20 elements or 1,000. It's better to use Array.every to check all elements for a condition. Here you could check if every element equals the first element.

It's better to test for strict equality (===) to avoid implicit type conversion. Note that 10 == '10' returns true, but 10 === '10' returns false.

Returning true and false is redundant. You can simply return the condition itself.

So the function can be reduced to this:

function isEqual(arr) {
    return arr.every(val => val === arr[0]);
}

And now that the function accepts an array instead of individual values, you can also avoid extracting a1, ..., a5 separately and simply pass an Array.slice:

if (isEqual(record[i].slice(j, j+5))) {
    ...
}

And for the vertical check, use Array.map to get the \$j\$th element of each slice:

if (isEqual(record.slice(i, i+5).map(arr => arr[j]))) {
    ...
}
Source Link
tdy
  • 2.3k
  • 1
  • 10
  • 21

function isEqual(a1, a2, a3, a4, a5) {
    if (a1==a2 && a2==a3 && a3==a4 && a4==a5) {
        return true;
    } else {
        return false;
    }
}

With 5 elements, this is already quite verbose, but now imagine checking 20 elements or 1,000. It's better to use Array.every to check all elements for a condition. Here you could check if every element equals the first element.

It's better to test for strict equality (===) to avoid implicit type conversion. Note that 10 == '10' returns true, but 10 === '10' returns false.

Returning true and false is redundant. You can simply return the condition itself.

So the function can be reduced to this:

function isEqual(arr) {
    return arr.every(val => val === arr[0]);
}

And now that the function accepts an array instead of individual values, you can also avoid extracting a1, ..., a5 separately and simply pass an Array.slice:

if (isEqual(record[i].slice(j, j+5))) {
    ...
}