harish1231
harish1231

Reputation: 13

Why is continue making the iteration stop?

I don't know why the iteration is stopping once I place a continue. If I replace continue with console.log() it's working fine. What I am trying to do is to return true if all elements are the same, and false otherwise.

function isUniform(de) {
   for(var i=0;i<de.length;i++) {
       if (de.indexOf(de[i])===0) {
            continue;
        }
        else {
            return false
        }
    }
    return true;
}

arr =[1,1,1];
isUniform(arr);

Upvotes: 1

Views: 76

Answers (1)

junvar
junvar

Reputation: 11584

As mentioned in the comments, you're predicate has a typo:

de.indexOf(de[i]) === 0

Also, this may be simpler to implement as:

let isUniform = arr => arr.every(a => a === arr[0])

console.log(isUniform([1, 1, 1, 1, 1]));
console.log(isUniform([1, 3, 1, 1, 1]));

In english, this is checking if every element of arr is equal to arr[0].

Upvotes: 2

Related Questions