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

233
Visualizações
how to make rows and columns of the same image using JS?

I would like a picture to appear x number of times in rows and columns. So if I'm creating a multiplication game and the question is "What is 9 x 5?", how do I ensure my 200x200 photo appears 45 times in 9 columns and 5 rows using vanilla JS? I figure I need to make a table, but don't know how to do that based on what the two numbers are.

let one = Math.floor(Math.random() * 9) + 1;
let two = Math.floor(Math.random() * 9) + 1;
let photos = document.getElementById('photos');

let product = one * two;
let productNum = parseInt(product);

const img = new Image();
const imgSrc = x.jpg;

function generateImage() {
  const img = document.createElement('img')
  img.src = imgSrc;
  return img;
}

for (let i = 0; i < productNum; i++ ) {
   photos.appendChild(generateImage());
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could create a function like this:

function createTable(w, h, src) {
    let table = document.createElement('table');
    for(let i = 0; i < h; ++i) {
        let row = table.insertRow();
        for(let j = 0; j < w; ++j) {
            let td = row.insertCell();
            let img = document.createElement('img');
            img.setAttribute('src', src);
            td.appendChild(img);
        }
    }
    document.getElementById('photos').appendChild(table);
}

createTable(9, 5, 'https://i.imgur.com/DfEE9nm.png');
<html>
<body>
<div id='photos'></div>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

The easiest way would probably be to create a div for each row and add images to each row to make up the columns.

let one = Math.floor(Math.random() * 9) + 1;
let two = Math.floor(Math.random() * 9) + 1;
let photos = document.getElementById("photos");

let product = one * two;
let productNum = parseInt(product);

const imgSrc = "https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico?v=ec617d715196";

function generateImage() {
  const img = document.createElement("img");
  img.src = imgSrc;
  return img;
}

const rows = one;
const columns = two;

for (let i = 0; i < rows; i++) {
  const rowDiv = document.createElement("div");
  for (let j = 0; j < columns; j++) {
    rowDiv.appendChild(generateImage());
  }

  photos.appendChild(rowDiv);
}
<div id="photos"></div>

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