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

278
Vistas
Multiplication Table in Javascript space between numbers?
 var numbers = ""; 
    for (var i = 1; i <= 13; i++){
      for (var j = 1; j<= 13; j++){
          numbers += (i*j) + '';
       }
       numbers += '<br>';
    }

element.innerHTML = numbers;

how can i make a space between every number?

example: enter image description here

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use <table> for that to structure your text:

let numbers = "<tbody>"; 
for (let i = 1; i <= 13; i++){
  numbers += '</tr>';
  for (let j = 1; j<= 13; j++){
      numbers += `<td>${i*j}</td>`;
  }
  numbers += '</tr>';
}
numbers += "</tbody>"
const element = document.querySelector("#element");
element.innerHTML = numbers;
td {
  text-align: right;
  width: 2em;
}
<table id="element">

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could be ambitious and, like David said, use CSS to take the weight of how the data is meant to be displayed. I've used CSS grid in this example which means you only need to have one loop, and the CSS takes care of the rest.

const arr = [];

const size = 13;
const limit = size * size;

// Push an HTML template string into
// the array
for (var i = 1; i <= limit; i++) {
  arr.push(`<div>${i}</div>`);
}

// Add the joined array (it makes an HTML string
// to the element with the `grid` class
const grid = document.querySelector('.grid');
grid.innerHTML = arr.join('');
.grid {
  display: grid;
  grid-template-columns: repeat(13, 30px);
  grid-gap: 0.2em;
}

.grid div {
  border: 1px solid #cdcdcd;
  font-size: 0.8em;
  background-color: #efefef;
  text-align: center;
  padding: 0.5em 0;
}
<div class="grid"></div>

Additional documentation

  • Template/string literals

Edit: you can even create a function that will accept a number, and, using CSS variables, the function will work out the size of the grid required and return the HTML.

function createGrid(columns) {

  const arr = [];
  const grid = columns * columns;

  document.documentElement.style.setProperty('--columns', columns)
  
  // Push an HTML template string into
  // the array
  for (var i = 1; i <= grid; i++) {
    arr.push(`<div>${i}</div>`);
  }

  return arr.join('');
}

const grid = document.querySelector('.grid');
grid.innerHTML = createGrid(5);
:root {
  --columns: 13;
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--columns), 40px);
  grid-gap: 0.2em;
}

.grid div {
  border: 1px solid #cdcdcd;
  font-size: 0.8em;
  background-color: #efefef;
  text-align: center;
  padding: 0.5em 0;
}
<div class="grid"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use grid approach with some CSS variables to make cell elements, for more details see comments in Code snippet:

Also you could render headings of the cells.

const gridSize = 13;

// get grid element
const grid = document.querySelector('.grid');

// create temporary wrapper
const fragmentWrapper = document.createDocumentFragment();

const renderTop = () => {
  for (var i = 0; i <= gridSize; i++) {
    if (i === 0) {
      // render mult char
      renderCell('x');
      
      continue;
    }
  
    renderCell(i, true);
  }
}

const renderGrid = () => {
  // append cells to wrapper
  for (var i = 1; i <= gridSize; i++) {
    for (var j = 0; j <= gridSize; j++) {
      if (j === 0) {
        // render heading cell
        const num = i;
        renderCell(num, true);
        
        continue;
      }
      
      // render default cell
      const num = i * j;
      renderCell(num);
    }
  }
}

const renderCell = (num, select = false) => {
  // create temporary cell
  const cell = document.createElement('div');
  cell.className = 'cell';
  // if requested to highligh cell
  select && cell.classList.add('select');
  cell.innerText = `${ num }`;

  // add cell to grid 
  fragmentWrapper.append(cell);
}

renderTop();
renderGrid();

// set grid size
grid.style.setProperty('--grid-size', gridSize);

// render grid
grid.append(fragmentWrapper);
.grid {
  display: grid;
  grid-template-columns: repeat(calc(var(--grid-size) + 1), 1fr);
  /* using CSS grid size (set in JS) */
  gap: .25rem;
}

.cell {
  display: flex;
  align-items: center;
  /* align content */
  justify-content: center;
  /* align content */
  aspect-ratio: 1/1;
  /* make cell square */
  border: 1px solid #d4d4d4;
  /* show cell borders */
}

.cell.select {
  background-color: tomato;
  color: white;
  font-weight: bold;
}
<div class="grid"></div>

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