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

209
Views
How can I change the order of the box numbers?

The order of the numbers in my box is as follows:

function boxNumbers(){
    let boxes = document.querySelectorAll('.box')
    boxes.forEach((box,i)=>{
    
        if(String(i).length==1 || (String(i).length==2 && Number(String(i)[0]))%2==0){
            //box.innerHTML = `${100-i}, i=${i}`
    
          box.innerHTML = 100-i 
        }
        else{
            box.innerHTML = String(Number(`${9-Number(String(i)[0])}${String(i)[1]}`)+ 1) 
    
          
        }
    })
    
    }

box1

how can I change it to look like this:

box2

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use this:

function boxNumbers() {
    let boxes = document.querySelectorAll('.box');
    let n = Math.sqrt(boxes.length);
    [...boxes].reverse().forEach((box, i) => {
        box.textContent = i % (n * 2) < n ? i + 1 : i + n - 2*(i % n);
    })
}

With the assignment to n you make it a bit more generic -- still assuming your table is square. By reversing the iteration, you eliminate the need for the 100- subtraction. What remains is a formula that detects whether we're on a row with a reverse sequence or not, and adapts the number accordingly. The number "1" will always be in the bottom-right corner:

function boxNumbers() {
    let boxes = document.querySelectorAll('.box');
    let n = Math.sqrt(boxes.length);
    [...boxes].reverse().forEach((box, i) => {
        box.textContent = i % (n * 2) < n ? i + 1 : i + n - 2*(i % n);
    })
}

// Utility to create the table
function fillTable(table, n) {
    for (let i = 0; i < n; i++) {
        let row = table.insertRow();
        for (let j = 0; j < n; j++) {
            let cell = row.insertCell();
            cell.className = "box";
        }
    }
}

// Example run with n=5. Adapt as needed
let n = 5
fillTable(document.querySelector('table'), n);
boxNumbers();
table { border-collapse: collapse }
td { border: 1px solid ; width: 20px; height: 20px; text-align: center }
<table></table>

about 4 years ago · Juan Pablo Isaza Report

0

Here is a function which builds a bi-dimensional array and appends it as a table (row/col) to a dom element. You can adapt it to your template as you wish. Works with any base number, yours is 5

function buildMatrix(baseNumber){
  var flip = false;
  var countDownNumber = baseNumber * baseNumber;
  var currNumber = countDownNumber;
  var matrix = "";
  
  for(i = 0; i < baseNumber; i++) {
    if(i !== 0){
        currNumber = (flip)? countDownNumber + 1 - baseNumber : countDownNumber;
    }
    matrix += "<tr>";
    
    for(j = 0; j < baseNumber; j++){
       matrix += "<td>" + currNumber + "</td>";
      
       // depending on the direction (flip) we increment or decrement
       (flip)? currNumber++ : currNumber--; 
       countDownNumber--;
    }
    
    // change direction at the end of a row
    flip = !flip;
    
    matrix += "</tr>";
  }
  
  return matrix;
}

var baseSquareNumber = 11; // here you put 5
var matrixHtml = buildMatrix(baseSquareNumber);

document.getElementById("matrix").innerHTML = matrixHtml;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    
    <table id="matrix">
    </table>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Report
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!