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

222
Views
Intentar crear una matriz con números aleatorios en JavaScript produce una matriz donde todas las filas son iguales

Cuando trato de crear una matriz con números aleatorios, todas las filas de la matriz aparecen iguales. ¿Cómo puedo solucionar esto? El código del problema es:

 var sudokuRowMatrix = []; var numbers = []; var randomizedArray = []; var sudokuColumnArray = []; for(i=0;i<=8;i++){ numbers[i]=i+1; sudokuRowMatrix[i] = []; sudokuColumnArray[i] = []; } function RandomizeArray(array){ for(arrayIndex=0;arrayIndex<=8;arrayIndex++){ randomArrayIndex = Math.floor(Math.random()*9); randomArrayIndex2 = Math.floor(Math.random()*9); placeHolder = array[randomArrayIndex]; array[randomArrayIndex]=array[randomArrayIndex2]; array[randomArrayIndex2] = placeHolder; } return array; } for(rowNumber=0;rowNumber<=8;rowNumber++){ sudokuRowMatrix[rowNumber] = RandomizeArray(numbers); }

Todas las filas de la matriz son iguales.

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

0

Cuando llamas a RandomizeArray(numbers) , pasas la matriz de números como referencia. Esto significa que siempre baraja la misma matriz y la configura como una fila en su matriz de Sudoku y, por lo tanto, baraja todas sus filas, ya que todas tienen la misma referencia.

Una manera fácil de solucionar esto es desestructurar su matriz antes de pasarla a su método: sudokuRowMatrix[rowNumber] = RandomzieArray([...numbers]);

Puedes encontrar mi fuente aquí.

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!