In this quiz, I am collecting question numbers and answers to store in an array. The goal is to replace old answers with new ones if a person changes their response on the quiz. If a question hasn't been answered yet, it should be added to the array at the appropriate index location.
let questions = [];
function get_id(xd,jf){
let question = (xd.parentNode.id);
let question_num = document.getElementById(question).getAttribute("value");
let number = document.getElementById(jf).getAttribute("value");
let new_answer = {Question:question_num, Answer:number};
questions.push(new_answer);
let found = questions.includes(new_answer);
console.log(found);
if (found == false) {
questions.push(new_answer);
};
// if (questions.some... " i couldn't understand how to use the some method"
//for (i = 0; i < questions.length; i++) { "and was thinking about using a for loop somehow."
console.log(questions);
};
I attempted to use the includes method to compare new and old answers, but it always returned true regardless of actual comparison results.
Below are other ideas I tried, but being new to programming, I'm unsure of the approach.
Here's the HTML structure of a single question:
<div class="row" id="question_1" value="0">
<div class="col-sm-2">
<div class="A">
how are you?
</div>
</div>
<div class="col-sm-2" id="answer5" onclick="get_id(this, id)" value="10">
<button id="answer5" value="10">
5
</button>
</div>
<div class="col-sm-2" id="answer4" onclick="get_id(this, id)" value="8">
<button id="answer4" value="8">
4
</button>
</div>
<div class="col-sm-2" id="answer3" onclick="get_id(this, id)" value="5">
<button id="answer3" value="5">
3
</button>
</div>
<div class="col-sm-2" id="answer2" onclick="get_id(this, id)" value="2">
<button id="answer2" value="2">
2
</button>
</div>
<div class="col-sm-2" id="answer1" onclick="get_id(this, id)" value="0">
<button id="answer1" value="0">
1
</button>
</div>
</div>
I haven't utilized button properties beyond styling them.