I want to get the user selection for a quiz.
I am doing this by using const choiceA = document.getElementById("A").value;
There are four choices and instead of creating 4 different const could I do something like. const choice = document.getElementById("A", "B"....).value
Or is there any other way to do this short hand?
Any links to good information about gathering user input would be much appreciated too :)
<html>
<form id="formEl">
<h2 id="question"></h2>
<button id="A" type="button" class="userSelection"></button>
<button id="B" type="button" class="userSelection"></button>
<button id="C" type="button" class="userSelection"></button>
<button id="D" type="button" class="userSelection"></button>
<button id="previous" type="button" class="userSelection">Previous</button>
<button id="next" type="button" class="userSelection">Next</button>
<button id="submit">Submit</button>
</form>
<js>
class Question {
constructor(question, ansA, ansB, ansC, ansD, answer) {
this.question = question;
this.ansA = ansA;
this.ansB = ansB;
this.ansC = ansC;
this.ansD = ansD;
this.answer = answer;
};
checkAns(ansSelected, answer) {
if (ansSelected === answer) {
console.log('Well Done')
};
};
};
//Questions
var questionOne = new Question('Where is Creete?', 'Barcalona', 'Greece', 'Dubi', 'Ireland', 'Greece');
var questionTwo = new Question('How many times have Liverppool won the Champions Legue?', '1', '4', '6', '5', '6');
var questionThree = new Question('Where was the first Godfather in the mafia from?', 'Milan', 'Gunoa', 'Rome', 'Napoli', 'Napoli');
//Index of the array with the questions array
var i = 0;
const arrayQuestion = [questionOne, questionTwo, questionThree];
//Selecting the value of the user once clicked
const choiceA = document.getElementById("A").value;
const choiceB = document.getElementById("B").value;
const choiceC = document.getElementById("C").value;
const choiceD = document.getElementById("D").value;