2

So I have an array which has 3 possible values: 0, 1 or 2

var a = [0,0,0];

I want to check if a[0], a[1], and a[2] all have the equal value of "1" or "2". How can I do this?

So 0,0,0 would return false, 1,2,1 false, but 1,1,1 or 2,2,2 would be true.

3
  • and why is 0,0,0 false? Commented Mar 4, 2015 at 12:21
  • Because I am making a game, and it has to check if player 1 or player 2 owns all 3 of the objects. So 0 doesn't count. Commented Mar 4, 2015 at 12:22
  • try this stackoverflow.com/questions/14832603/… but this only check if all are equal, then you have to check a[0] is 0 or 1/2 Commented Mar 4, 2015 at 12:24

4 Answers 4

3

Try this:

function checkArray(array){
    var firstElement = array[0];
    if(!firstElement) return false;

    var result = true;

    array.forEach(function(elem){
        if(elem != firstElement) result = false;
    });

    return result;
}
Sign up to request clarification or add additional context in comments.

8 Comments

You could easily do if(!array.length) return false; instead of assigning firstelement, though. This is how I'd do it but I kind of like the reduce function below.
Semmantics, but you could just return false inside the foreach loop and save the effort of continuing to look when you already know it's false.
about if(!array.length) return false; i think we must check array to null, 0 or undifined
Okay, thats good I guess. I cant see your updated every second :) Your scripts works though, and is easily understood. Me like.
This is exactly what I was trying to do. Thank you.
|
2

The regex solution was slow as shown in http://jsperf.com/looping-vs-regex. SO came up with this.

function checkArr(arr){
   var f = arr[0];
   if(f == 0) return false;
   for(var i = 0; i < arr.length; i++)
      if(arr[i] != f) return false;
   return true;
}

15 Comments

Theres always someone suggesting regex patterns for everything :)
@somethinghere When it makes your work easy and more readable, why not?
I am kind of new to Javascript, so how can I make this script read my array? Where do I put the array name.
I've never found regex to be "more readable" but easier, sure.
@somethinghere - true, but for this particular use, other options are either loop over an array, or use reduce/filter in some non-obiovus ways. Regex actually is pretty readable, when compared to other options.
|
0

Try this:

var validate = function(arr) {
    var s = 0;
    var i = l = arr.length;
    while(i --)
        s += arr[i];
    return l === s || (2 * l) === s;
};

console.log(validate([0,0,0]));//false
console.log(validate([1,2,1]));//false
console.log(validate([2,2,2]));//true
console.log(validate([1,1,1]));//true
console.log(validate([2,2,2,2,2]));//true

Comments

0

Another apporach, without looping or regexes:

var arr = [1,2,1];
arr.every(function (el) {
  return (el === 1 || el === 2) && el === arr[0];
})

Or, with .some instead of .every:

!arr.some(function (el) {
  return (el !== 1 && el !== 2) || el !== arr[0];
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.