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

148
Views
Comparación de dos matrices en Javascript para encontrar similitudes idénticas y otras similitudes

Tengo dos matrices en Javascript: código y código enviado. Estoy tratando de comparar las dos matrices. Cada una de las matrices tiene 4 enteros y cada entero es un valor aleatorio de 1 a 6. También tengo dos variables: rojo y blanco. Cuando se comparan los dos, la variable roja debe establecerse en el número de similitudes en las matrices que tienen el mismo número, el mismo índice. El blanco debe establecerse en el número de similitudes en la matriz que son el mismo número, pero índices diferentes. Por ejemplo, si la matriz de código es [1, 3, 6, 5] y el código enviado es [1, 6, 4, 5], el rojo se establecería en 2 y el blanco en 1. Esto es lo mismo lógica como el juego Mastermind si alguien ha jugado eso. A continuación se muestra lo que he intentado, pero no funciona según lo previsto.

 for(let i = 0; i < code.length; i++) { if(code[i] == submittedCode[i]) { code.splice(i, 1); submittedCode.splice(i, 1); red++; //console.log(i); } } console.log(code); var saveLength = code.length; code = code.filter(function(val) { return submittedCode.indexOf(val) == -1; }); white = saveLength - code.length; console.log(red + ", " + white);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let arr=[1,3,1,2] //two array we operate on let arr2=[4,4,1,2] let red=0 let white=0 //we need to check the current length of remaining array let currentLength=arr.length for(let i=0; i<currentLength; i++){ if(arr[i]===arr2[i]){ arr.splice(i,1) //if same number same index, remove this item from array arr2.splice(i,1) currentLength-=1 //currentLength-1 because we reduced the array i-=1 //i-1 because we'd skip the next element red+=1 //update red } } //we basically do the same thing but with white //but in the second array we look for the first index of the current element and remove that for(let i=0; i<arr.length; i++){ if(arr2.includes(arr[i])){ //1: I should've taken away the item from arr2 first //2: arr2.splice(arr2.findIndex(f=>f===arr[i],1)) notice I messed up where I put the 1 at the end arr2.splice(arr2.findIndex(f=>f===arr[i]),1) arr.splice(i,1) i-=1 white+=1 } }

Ahora, esta podría no ser la solución más óptima, puede hacer esto en un ciclo, pero para mayor visibilidad, creé 2 ciclos for, en el primero verificamos los elementos 'rojos', y los eliminamos en el segundo ciclo verificamos si hay elementos 'blancos', y los eliminamos.

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!