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

239
Views
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 answers
Answer question

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 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!