Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
Trying to create a matrix with random numbers in JavaScript produces a matrix where all the rows are the same

When I try to create a matrix with random numbers, the matrix rows all appear the same. How do I fix this? The code for the problem is:

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);
}

All the rows of the matrix are the same

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When you call RandomizeArray(numbers) you pass the numbers array as a reference. This means, that you always shuffle the same array and set it as a row in your Sudoku matrix and therefore shuffle all of your rows since they all hold the same reference.

An easy way to fix this, is to destructure your array before passing it to your method: sudokuRowMatrix[rowNumber] = RandomzieArray([...numbers]);

You can find my source here.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda