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

166
Vistas
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 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