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

243
Vistas
I'm creating a quiz with 5 random questions (out of an array with 20). I want to assign a picture to every possible question

I'm creating a quiz with 5 random questions (out of an array of 20). I want to assign a picture to every possible question. The way I did that was using switch statements but that produces too much code as I have 20 questions. Is there another way to achieve this by using less code?

Here is the code I'm using:

const assignPictures = (value, questionID) => {

const addPicture = document.createElement("div");

 addPicture.setAttribute("class","imagesInQuestion")

  switch(true){
     
   case value === Array[0]:

      addPicture.innerHTML = `
   <img class="inlineImages" src ="/Images/Image.png">              
`
      break;
  
   case value === Array[1]:

      addPicture.innerHTML = `
      <img class="inlineImages" src ="/Images/Image2.png">              
   `
      break;

.....

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Use an object array.

var data = [{
      question : "Question"
      answer : "answer"
      image : "/image.png"
    },
    {
      question : "Question"
      answer : "answer"
      image : "/image.png"
    }]

Then create functions for inserting images and questions while clicking the button

over 4 years ago · Santiago Trujillo 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