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

223
Views
I'm trying to create a quiz with a picture for each question. Im using switch statement. Is a way to simplify the code as I have 20 questions?
const assignPictures = (value, questionID) => {
  const addPicture = document.createElement("div");
  addPicture.setAttribute("class","imagesInQuestion")

  switch(true){
    case value === secondArray[0]:
      addPicture.innerHTML = `<img class="inlineImages" src ="/Images/Block1.png">`
      break;

    case value === secondArray[1]:
      addPicture.innerHTML = `<img class="inlineImages" src ="/Images/Block2.png">`
      break;
   }
   return document.querySelector(questionID).append(addPicture);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Based on OP's comment, you could use an array to associate each question with a path to a file. Then, you would use this array to display the good image based on the current question index:

const assignPictures = (value, questionID) => {
    // each item is the path to the relevant image
    const images = [
        '/Images/Block1.png',
        '/another_path.jpg',
        'https://external.image.jpg',
    ];

    // if `questionID` is not a numerical index, adjust `images` accordingly:
    // const images = {
    //     firstQuestion: '/Images/Block1.png',
    //     anotherID: '/another_path.jpg',
    //     whyNot: 'https://external.image.jpg',
    // }

    const addPicture = document.createElement("div");
    addPicture.setAttribute("class", "imagesInQuestion")
    // assuming `questionID` is the current question number
    addPicture.innerHTML = `<img class="inlineImages" src ="${images[questionID]}">`
    document.querySelector(questionID).append(addPicture);
}

Please, note that we are using backticks to create a template literal:

// This allows us to use this syntax:
`<img class="inlineImages" src ="${images[questionID]}">`

// rather than this one:
'<img class="inlineImages" src ="' + images[questionID] + '">';
about 4 years ago · Juan Pablo Isaza 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!