0
var player1=["t1", "t9", "t7", "t8", "t2"];
var player2=["t5", "t3", "t4", "t6"];
var winmoves=[[t1,t2,t3],[t4,t5,t6],[t7,t8,t9],[t1,t4,t7],[t2,t5,t8],[t3,t6,t9],[t1,t5,t9],[t3,t5,t7]];

    else if (moves>=3 && moves<=9) {
                    moves+=1;
                    if (turn==1) {
                        var i=0;
                        turn=2;
                        x.innerHTML="<img src='x.png'/>";
                        player1.push(x.id);
                        while(i<=moves){
                            if (winmoves[i] in player1) {
                                alert("player1 wins");
                                document.getElementById("player1").innerHTML=1;
                                }
                            i+=1;
                        }
                    }

i have 1d array player1 and 2d array winmoves in javascript and i want to check that w[0]'s all values are present in p and so on for w[1],w[2],etc. if condition with (winmoves[i] in player1) is not working. i don't know if i am writing this write. help me guys i am stuck here how can i do so.

It is not working even after i have made these changes.

else if (moves>=3 && moves<9) {
                        moves+=1;
                        if (turn==1) {
                            var i=0;
                            turn=2;
                            x.innerHTML="<img src='x.png'/>";
                            player1.push(x.id);
                            while(i<=moves){
                                 mapped1 = winmoves.map(a1 => a1.every(e1 => player1.includes(e1)));
                                if (mapped1[i]) {
                                    alert("player1 wins");
                                    document.getElementById("player1").innerHTML=1;
                                    }
                                i+=1;
                            }
                        }
                        else if (turn==2) {
                            turn=1;
                            var i=0;
                            x.innerHTML="<img src='o.png'/>";
                            turn=1;
                            player2.push(x.id);
                            while(i<=moves)
                            { 
                                 mapped2 = winmoves.map(a => a.every(e => player2.includes(e)));
                                if (mapped2[i]) {
                                    alert("player2 wins");
                                    document.getElementById("player2").innerHTML=1;
                                    }
                                i+=1;
                            }   
                        }

                    }

2 Answers 2

1

I would simply do this with a simple invention of Array.prototype.intersect() and the rest is such a straightforward single liner.

Array.prototype.intersect = function(a) {
  return this.filter(e => a.includes(e));
};

var player1 = ["t1","t2","t3","t5","t7"],
   winmoves = [["t1","t2","t3"],["t4","t5","t6"],["t7","t8","t9"],["t1","t4","t7"],["t2","t5","t8"],["t3","t6","t9"],["t1","t5","t9"],["t3","t5","t7"]];
   filtered = winmoves.filter(a => a.intersect(player1).length == a.length);
     mapped = winmoves.map(a => a.intersect(player1).length == a.length);
console.log(filtered);
console.log(mapped);

OK we have a generic Array method which finds the intersection of two arrays. (inbetween the array it's called upon and the one provided as argument) It's basically a filter checking each item of first array to see whether it is included in the second array. So we filter out the items exist in both arrays.

To obtain the filtered array we utilize Array.prototype.filter() again. This time our first array is winmoves which includes arrays that we will check for each the intersection with player1 array. If the intersection length is equal to winmove's item's length that means all elements of winmove's item is existing in the player1 array. So we return that array item to the filtered.

Specific to your case without using an intersect method you can utilize Array.prototype.every() as follows;

var player1 = ["t1","t2","t3","t5","t7"],
   winmoves = [["t1","t2","t3"],["t4","t5","t6"],["t7","t8","t9"],["t1","t4","t7"],["t2","t5","t8"],["t3","t6","t9"],["t1","t5","t9"],["t3","t5","t7"]];
   filtered = winmoves.filter(a => a.every(e => player1.includes(e)));
     mapped = winmoves.map(a => a.every(e => player1.includes(e)));
console.log(filtered);
console.log(mapped);

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

8 Comments

can u please explain what is happing in this snippet.
@Rohit Chopra i added an explanation to the answer for your information.
thanks Redu.. but what if i don't want filtered array. instead of it simple true or false.
@Rohit Chopra just added that to the snippet too. It's just done by replacing filter with map.
can we do this by using Array.prototype.every()?
|
0

What you could do is use nested for loops

for(var j = 0, j < elemInP, j++){
    int flag = 0;
    for(var x = 0, x < elemInWx, x++){
        for(var y = 0, y < elemInWy, y++){
            if(p[j] == w[x][y]){
                flag = 1;
                /*There is no need to run this loop once the value has been found*/
                break;
            }
        }
        if(flag){
            /*If we have found the value no need to keep looking*/
            break;
        }
    }
    if(!flag){
       print p[j] is not in w;
    }
}

This is just a general idea of one way to compare the two arrays. The actual syntax of the code will need to be edited to work in JavaScript as this is just basic pseudocode. Flag is just a variable that holds if the value was found or not and is reset for each new value. For efficiency, you could have a break/return after setting flag = 1, but this is

Comments

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.