1

So, I have an array that looks like this:

[ ["","",""], ["","",""], ["","",""] ]

How would I test it in javascript to see if, perhaps, the 1st index was filled with X's?

EX: [ ["X","X","X"], ["","",""], ["","",""] ]

I keep thinking I should do something like this, but it feels like there would be a quicker way...

var counter = 0, 
    win,
    b = [ ["X","X","X"], ["","",""], ["","",""] ],
    num = b[0].length;
for(var i=0; i<num; i++){
    if(b[0][i]==="X"){ counter++; }
}
if(num===counter){ win=true; }

8 Answers 8

2
var win = b[0].join('') === 'XXX';
Sign up to request clarification or add additional context in comments.

5 Comments

I understand, I but don't want people to just write code for me. I'd prefer to know a foundation and expand off of it :-). Thank you for the help. The simplest answers are often the finest.
Just add a loop for var 0, tests every index in the rows. Then add some modified diagonal math, etc etc. I got the answer I wanted here. Ty.
Decided to write a few functions and create a switch statement to determine the computer move. Thanks again!
Or just do var win = b[0].join('') === new Array(b[0].length + 1).join('X'); if you really like this technique.
@Scott, that's my answer xD
1

Use Array.prototype.every. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

function isAnX(c) { return c === 'X'; }
var win = b[0].every(isAnX);

1 Comment

Totally interesting concept. I'll likely expand on this instead of the upvoted post above. Well done and thank you.
1
win = true;
for (var i = 0; i < num; i++) {
    if (b[0][i] != "X") {
        win = false;
        break;
    }
}

1 Comment

Hahaha, shoulda thought to reverse it this way. Nice answer :-)
1

Here's how to test if an array contains identical elements:

allTheSame = someArray.every(function(x, _, ary) { return x == ary[0] })

see every

or, for pre-ES5 engines:

allTheSame = function(ary) {
    for(var i = 1; i < ary.length; i++)
       if(ary[i] != ary[0]) return false;
    return true;
}

3 Comments

What does the underscore represent? Just a blank filler to dig deeper?
@NicholasHazel: an unused parameter (index).
Ahhh, figured as much. Thanks for your answer :-)
0

use Array.prototype.reduce

 var win=b[0].reduce(function (v, c) { return v && (c=="X") }, true); 

2 Comments

every is slightly preferable, because it will quite once it finds a false.
I mentioned .reduce because is more flexible. But the performance hit you pointed out is a very good reason to prefer .every thanks.
0

Another one, just to give one more option:

var win = new RegExp("^X{"+b[0].length+"}$").test(b[0].join(''));

Cheers

1 Comment

I've been told that regex expressions are a slow way to process information. I use them for basic properties typically (converting timestamps, etc), but for this case I don't think this is the correct solution. Awesome try, and something I use regularly though :-)
0

Similar to other answers, but with something of a twist:

var all = function(val, arr) {
    var fn = function(arr) {return arr.every(function(item) {return item === val;});};
    return arguments.length > 1 ? fn(arr) : fn;
};

var win = all('X', b[0]); //=> true
// or
var allXs = all('X');
var win = allXs(b[0]); //=> true

Or if you just want to know if they're all the same:

var allAlike = function(arr) {
    return (arr.length == 0) || all(arr[0], arr);
}

var win = allAlike(b[0]);

This would be easier if you had an appropriate curry-like function available.

2 Comments

So you're breaking the check into other functions. Perhaps would suit my purposes. Thank you :-)
@NicholasHazel: Yes, breaking it into reusable functions. This really only makes sense if you might actually have additional uses for these functions, though. Most of the other solutions would be better if you have no further use for all or allAlike.
0

Hahaha, this is cool:

var win = Array(b[0].length+1).join('X') == b[0].join('')

Cheers!

1 Comment

Much better for my purposes of the site I'm building it on than your previous post.

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.