'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;
.....
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mplungjan |
