-1

I am having trouble witht he following piece of code:

function isUniform (arr) {
    arr.forEach (function (el){
        console.log (el);
        if (arr [0] !== el) {
            return (false);
        }
    })
    return (true);
}

console.log (isUniform ([1, 1, 1, 1]));
console.log (isUniform ([2, 1, 1, 1]));
console.log (isUniform (["a", "b", "p"]));
console.log (isUniform (["b", "b", "b"]));

It was supposed to return "true" when all elements in an array are identical, and "false" otherwise, but it keeps returning "true", and through testing I found out that JavaScript skips over the only "if" statement.

EDIT: Not a duplicate because I'm asking for advice on my own code. Specifically about why the "if" statement nested inside forEach was being ignored, which isn't covered in other questions.

4 Answers4

1
[1,1,1,1].every( (val, i, arr) => val === arr[0] )  //True
[1,1,2,1].every( (val, i, arr) => val === arr[0] )  //False

as mentionned in this post

Sebastien D
  • 4,369
  • 4
  • 18
  • 46
0

.forEach ignores the result of the callback, and is the wrong tool for the job since you're basically reimplementing .every(), which has the additional advantage of halting the iteration as soon as it knows it can't return true.

function isUniform (arr) {
    return arr.every(function (el){
        return arr[0] === el
    });
}

console.log (isUniform ([1, 1, 1, 1]));
console.log (isUniform ([2, 1, 1, 1]));
console.log (isUniform (["a", "b", "p"]));
console.log (isUniform (["b", "b", "b"]));

If you were going to implement it yourself, I'd use a for-of loop, so that you can return out of it.

function isUniform (arr) {
    for (const el of arr) {
        if (el !== arr[0]) {
            return false;
        }
    }
    return true;
}

console.log (isUniform ([1, 1, 1, 1]));
console.log (isUniform ([2, 1, 1, 1]));
console.log (isUniform (["a", "b", "p"]));
console.log (isUniform (["b", "b", "b"]));
  • Why would returning out of it be desirable, when I can use the first snippet? – Lucas Assis Feb 14 '18 at 18:30
  • Because it returns from the `isUniform` call instead of from a callback that has no use for the return value. The `.every()` is different in that it does use the callback's return value as a signal to stop iterating when `false` is given. –  Feb 14 '18 at 19:17
0

The return false; within forEach function is only breaking the passed callback.

Add a variable to store the boolean value.

function isUniform (arr) {
    var result = true;
    arr.forEach (function (el) {
        console.log (el);
        if (arr [0] !== el) {
            result = false;
        }
    });

    return result;
}
Ele
  • 33,468
  • 7
  • 37
  • 75
0
function isUniform (arr) {
  let flag = true;

  arr.forEach (function (el){
    console.log (el);
    if (arr[0] !== el) {
      flag = false;
    }
  })
  return flag;
}

console.log (isUniform ([1, 1, 1, 1]));
console.log (isUniform ([2, 1, 1, 1]));
console.log (isUniform (["a", "b", "p"]));
console.log (isUniform (["b", "b", "b"]));
Tim Han
  • 166
  • 1
  • 12