Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda