Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!