JavaScript Code Help.
I am creating an online flashcard game quiz with java script. There are 20 images with 20 corresponding answers that identify what the picture is. The images are in one array, the answers are in another array.
The page displays 1 image at a time and is randomly chosen from the 20 images. Below the randomly generated image are 4 buttons. 3 that hold random answers while 1 is the correct answer.
How do I generate the 3 random answers and the correct answer? The correct answer for every image cannot be the same button location.
/**
* First create an array with images let say image_array
* Second create an array with the corresponding answere make sure you keep the answeres should be the corresponding index
* position of the image_array let say answere_array
*
* Create another array for the answere positions in which position among the four buttons your answere
* should be by doing this
* you can easily show the correct answere and you can determine your answere position
* each value in this array should be less than 4 because you have 4 choices like [0,2,1,3]
* let say correct_pos_array
*/
/** The below function will take position of an answere and return the positions of incorect answeres */
function getRandom3Positions(answer_pos){
let temp=new Set();
let num;
while(temp.size < 3){
num = Math.floor(Math.random() * 10)
if(num != answer_pos && num < 4 && !temp.has(num)){temp.add(num)}
}
return [...temp]
}
/**
*
* @param correct_ans_pos
* this function will return the wrong answere position for preparing the wrong choices
*/
function getRandomIncorrectAnsweresPos(correct_ans_pos){
let temp = new Set()
temp.add(correct_ans_pos)
while(temp.size <4 ){
temp.add(Math.floor(Math.random()*20))
}
temp.delete(correct_ans_pos)
return [...temp]
}
/**
* this is the code if you want to generate answere positions randomly
*
let temp=[]
while(temp.length <20){
let num = Math.floor(Math.random()*10)
if(num < 4) temp.push(num)
}
console.log(temp)
*/
function solver(){
let image_array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"];
let answere_array = ["A__ans", "B__ans", "C__ans", "D__ans", "E__ans", "F__ans", "G__ans", "H__ans", "I__ans", "J__ans",
"K__ans", "L__ans", "M__ans", "N__ans", "O__ans", "P__ans", "Q__ans", "R__ans", "S__ans", "T__ans"]
let correct_pos_array = [2, 3, 0, 0, 1, 1, 2, 1, 2, 1, 0, 1, 1, 2, 2, 3, 0, 0, 1, 2]
let question_array =[]
for(let i=0;i<image_array.length;i++){
let incorect_position = getRandom3Positions(correct_pos_array[i])
/**the above array we will get the incorrect positions */
let incorrect_ans_pos = getRandomIncorrectAnsweresPos(i);
/**Here the above line we will get the incorrect answere indexes */
// console.log("incorrect position : ",incorect_position," corpos : ",correct_pos_array[i])
// console.log("incorrect_ans_pos : ",incorrect_ans_pos," cur_pos : ",i)
/**
* Creating the temporary array of length 4 which represent 4 options one among them is correct and we can get
the position of correct answere from correct_pos_array
*/
let result = [0,0,0,0]
result[correct_pos_array[i]] = answere_array[i];
for(let j =0;j<incorect_position.length;j++){
result[incorect_position[j]] = answere_array[incorrect_ans_pos[j]]
}
let question_obj ={
question : image_array[i],
options:result
}
question_array.push(question_obj)
}
console.log(question_array);
}
solver()
Output:


JavaScript Code Help. I am creating an online flashcard game quiz with java script. There are...