0

Problem

I'm trying to create a function that evaluates an array and if every element inside the array is the same, it would return true and otherwise false. I don't want it to return true/false for each individual element, just for the entire array.

Attempt 1

This method works, but it returns true/false for each element in the array:

function isUniform(arr){
    let first = arr[0];
    for (let i = 1; i <arr.length; i++){
        if (arr[0] !== arr[i]){
            console.log(false);
        } else {
            console.log(true);
        }
    }
}

Attempt 2

This method returns true/false, once and then prints true again at the end:

function isUniform(arr){
    let first = arr[0];
    for (let i = 1; i <arr.length; i++){
        if (arr[0] !== arr[i]){
            console.log(false);
        }
    }
    console.log(true);
}

5 Answers 5

3

If you want to test if something is true for every element of an array, you don't really need to write much — you can use array.every for this and just compare the first element. every() is nice because it will return early if a false condition is found.

var arr1 = [1, 1, 1, 1, 1, 1, 1]
var arr2 = [1, 1, 1, 2, 1, 1, 1]

console.log(arr1.every((n, _, self) => n === self[0]))
console.log(arr2.every((n, _, self) => n === self[0]))

This will return true for an empty array, which may or may not be what you want.

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

5 Comments

This is the best answer
@KunalMukherjee, this is a different approach, but not an answer to the question.
@NinaScholz the OP is asking for if all the elements in the array are the same or not
Thank you! I'll keep this in mind in the future - right now I'm restricted to just using a for loop or for each, but I'm glad I learned this.
if only the question is to check, then is clearly a duplicate question and a matter of a close request.
2

Alternative using the object Set

new Set(arr).size === 1 // This means all the elements are equal.

let isUniform = (arr) => new Set(arr).size === 1;

console.log(isUniform([4,4,4,4,4]));
console.log(isUniform([4,4,4,4,4,5]));

Comments

1

Add a return statement with false and end the function. The return value could be used later.

function isUniform(arr) {
    let first = arr[0];
    for (let i = 1; i < arr.length; i++) {
        if (arr[0] !== arr[i]) {
            console.log(false);
            return false;
        }
    }
    console.log(true);
    return true;
}

For using a return value, you need to return true at the end, too.

1 Comment

THANK YOU SO MUCH. Ugh, it's always something stupidly obvious and simple like this.
1

Try with Array#every .its Checking all other value is same with first index of array

function isUniform(arr) {
  return arr.every(a=> a === arr[0])
}

console.log(isUniform([2,2,2,2]));
console.log(isUniform([4,4,4,4,4,5]));

Comments

0

The problem is that you need to stop once you've found the first false element:

function isUniform(arr){
    let first = arr[0]; 
    let uniform = true;
    for (let i = 1; i <arr.length; i++){
        if (arr[0] !== arr[i]){
            uniform = false; 
            break;
        }
    } 
    console.log(uniform);
}

1 Comment

break does not prevent to show true. it just exits the loop.

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.