Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
check if 2d array match or have the value of 1d array
let winningCombination = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7], 
[1,5,9]]
let o = [5,8,6,7]
let x = [1,2,3,4,9];
var xMark = "X"
var oMark = "O"

function checkWinner(){
if(winningCombination.some(item => item.every((val,index) => val === o[index]))){
  winner = oMark;
}else if (winningCombination.some(item => item.every((val,index) => val === x[index]))){
  winner = xMark;
}

 return winner;
}

how do I check if the 1d array matches the 2d array? when the x or o array gets larger the checkWinner method isn't working

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your code for checking if one team's marks is a winning combination is order dependent. It checks if the first move of the player is equal to the first move of the winning combination, even though order doesn't matter in rock paper scissors. It also won't check beyond the third move.

A fix would be to do something like:

winningCombination.some(item => item.every((val) => o.includes(val))

Or even shorter:

winningCombination.some(item => item.every(o.includes)

Or in English: For each winning combination, check every needed mark, and see if that mark is included ANYWHERE in the list of marks the player has set.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!