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

165
Visualizações
Trying to fill any cases in double array - javascript

I'm just starting in javascript, and for my debut I simply wanted to fill each cases in tab with 0. So I tried an approach that is familiar to me since I already had the opportunity to write code in C

let tab = [[], [], [], []]

function put_zero()
{
    for (var i = 0; i < 16; i++)
    {
        var x = i / 4;
        var y = i % 4;
        tab[x][y] = 0;
    }
}

function print_tab()
{
    for (var i = 0; i < 4; i++)
        if (tab[i].length <= 0)
            return;

    for (var i = 0; i < 16; i++)
    {
        var x = i / 4;
        var y = i % 4;
        if (x != 0 && y % 4 == 0)
            document.write("<br>");
        document.write(tab[x][y]);
    }
    document.write("<br>");
}

function tst(){
    put_zero();
    print_tab();
}

function myFunction() {
  var btn = document.createElement("BUTTON");
  btn.innerHTML = "CLICK ME";
  btn.onclick = tst();
  document.body.appendChild(btn);
}

But this way of doing with the i % 4 and i / 4 don't work in javascript but functional in C because before posting the question I checked anyway, so is there a reason for that? Because if I use a double loop it's ok

let tab = [[], [], [], []]

function put_zero()
{
    for (var i = 0; i < 4; i++)
        for (var j = 0; j < 4; j++)
            tab[i][j] = 0;
}

function print_tab()
{
    for (var i = 0; i < 4; i++)
        if (tab[i].length <= 0)
            return;

    for (var i = 0; i < 4; i++)
        for (var j = 0; j < 4; j++)
            {
                if (i != 0 && j % 4 == 0)
                    document.write("<br>");
                document.write(tab[i][j]);
            }
    document.write("<br>");
}

function tst(){
    put_zero();
    print_tab();
}

function myFunction() {
  var btn = document.createElement("BUTTON");
  btn.innerHTML = "CLICK ME";
  btn.onclick = tst();
  document.body.appendChild(btn);
}
about 4 years ago · Juan Pablo Isaza
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