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

99
Visualizações
Building Casino game in JavaScript

I'am building a casino game and ran into a problem. I have this code:

let images = [
document.getElementById("img1"),
document.getElementById("img5"),
document.getElementById("img9"),
document.getElementById("img11"),
document.getElementById("img13")

]

let sources = ["cherry.png", "lemon.png", "watermelon.png", "grape.png", "diamond.png", 
"luckycharm.png"]

So what I want to do here is to check if atleast 3 images has the same source. How can I do it?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can get the "src" attribute of a image using the .getAttribute("src") method, then you can compare them in JavaScript as strings. In your case, that would look like: images[0].getAttribute("src") === images[1].getAttribute("src")

Which could be: "cherry.png" === "lemon.png" and this would return false.

If it wasn't clear either, you should definitely use a for loop or array methods to go through all of your elements in images.

This could easily be accomplished with just a for loop and the .filter method:

function threeMatch() {
  sources.forEach(source => {
    if(images.filter(
      image => image.getAttribute("src") === source
    ).length === 3) return true;
  });
  return false;
}

about 4 years ago · Juan Pablo Isaza Relatório

0

I thinks creating an object with keys (image names) and values (array of dom elements that have the image) is a good approach for this application. You may want to know which image appears where.

let sources = ["cherry.png", "lemon.png", "watermelon.png", "grape.png", "diamond.png", 
"luckycharm.png"]

const images = ["cherry.png","cherry.png","watermelon.png","cherry.png","diamond.png"];

const makeChunks = () => {
  const chunks = {};
  
  images.forEach(
    (img, i) => img in chunks ? chunks[img].push(i) : chunks[img] = [i]
  );
  
  return chunks;
}

console.log(makeChunks())

const findTrio = () => {
  return Object.fromEntries(
    Object.entries(makeChunks()).filter((x) => x[1].length > 2)
  )
}

console.log(findTrio())

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