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

143
Visualizações
Javascript array won't change inside of for loop

I am trying to rotate a 2dimensional array in javascript

the code outside the forloop to switch the values works but when i try to do that inside the for loop the variables are not changing

i think it is some sort of reference problem and i searched for that but i can't find a solution to this problem

temp = tempboard[0][1];
    console.log("unchanged:"+tempboard[0][1]+""+tempboard[6][1]);
    tempboard[0][1] = tempboard[6][1];
    tempboard[6][1] = temp;
    console.log("changed:"+tempboard[0][1]+""+tempboard[6][1]);
    
    for(i = 0; i < board.length; i++){
        for(j = 0; j < board[0].length; j++){
            /*a = tempboard[i][j];
            b = tempboard[j][i];
            temp = a;
            a = b;
            b = temp;
            console.log(a+" "+b);*/
            temp = tempboard[i][j];
            console.log("unchanged:"+tempboard[i][j]+""+tempboard[j][i]);
            tempboard[i][j] = tempboard[j][i];
            tempboard[j][i] = temp;
            console.log("changed:"+tempboard[j][i]+""+tempboard[i][j]);
        }
    }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think it will be easier to build a new array based on the old one.

In the below code a new array is build in reverse based on the original one.

arr = [[11, 12, 13], [21, 22, 23], [31, 32, 33], [41, 42, 43]];

console.log(JSON.stringify(Rotate2D(arr)));

function Rotate2D(array) {
  //Create a new empty array with the same size as the original one.
  let returnArr = [...Array(array.length)].map(e => Array(array[0].length));
  let maxIndexI = array.length - 1;
  let maxIndexJ = array[0].length - 1;

  //Fill the return array rotated
  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array[0].length; j++) {
      returnArr[maxIndexI - i][maxIndexJ - j] = array[i][j]
    }
  }
  return returnArr;
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Try adding "let" keyword to your for loop, it could be an hoisting issue;

for(let i = 0; i < board.length; i++){
        for(let j = 0; j < board[0].length; j++){
            /*a = tempboard[i][j];
            b = tempboard[j][i];
            temp = a;
            a = b;
            b = temp;
            console.log(a+" "+b);*/
            temp = tempboard[i][j];
            console.log("unchanged:"+tempboard[i][j]+""+tempboard[j][i]);
            tempboard[i][j] = tempboard[j][i];
            tempboard[j][i] = temp;
            console.log("changed:"+tempboard[j][i]+""+tempboard[i][j]);
        }
    }
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