I need help to assign value of 'hmmm' variable inside xhr.onload fuction to the js variable 'questions' outside xhr function,the value of 'hmmm' is json object whose structure is similar to the 'questions' variable outside function. i have tried searching for it but haven't yet come across good solution.
is there any way to assign value of 'hmmm' variable to 'questions' variable
Much appreciated .
'''
let quiz = document.forms["welcome_form"]["quiz"].value;
const xhr=new XMLHttpRequest()
const method='GET'
const url="/quiz/dock/{{q.slug}}"
console.log(url)
const responseType='json'
xhr.responseType=responseType
xhr.open(method,url)
xhr.onload=function(){
console.log(xhr.response)
const Server_response=xhr.response
let hmm=Server_response.quiz_list
console.log(hmm)
}
xhr.send()
let questions = [
{
id: 1,
question: "What is the full form of RAM ?",
answer: "Random Access Memory",
options: [
"Random Access Memory",
"Randomely Access Memory",
"Run Aceapt Memory",
"None of these"
]
},
{
id: 2,
question: "What is the full form of CPU?",
answer: "Central Processing Unit",
options: [
"Central Program Unit",
"Central Processing Unit",
"Central Preload Unit",
"None of these"
]
},
{
id: 3,
question: "What is the full form of E-mail",
answer: "Electronic Mail",
options: [
"Electronic Mail",
"Electric Mail",
"Engine Mail",
"None of these"
]
}
];
console.log(questions)
let question_count = 0;
let points = 0;
window.onload = function() {
show(question_count);
};
function next() {
// if the question is last then redirect to final page
if (question_count == questions.length - 1) {
sessionStorage.setItem("time", time);
clearInterval(mytime);
window.location.href='/quiz/end/'+quiz
}
console.log(question_count);
let user_answer = document.querySelector("li.option.active").innerHTML;
// check if the answer is right or wrong
if (user_answer == questions[question_count].answer) {
points += 10;
sessionStorage.setItem("points", points);
}
console.log(points);
question_count++;
show(question_count);
}
function show(count) {
let question = document.getElementById("questions");
let [first, second, third, fourth] = questions[count].options;
question.innerHTML = `
<h2>Q${count + 1}. ${questions[count].question}</h2>
<ul class="option_group">
<li class="option">${first}</li>
<li class="option">${second}</li>
<li class="option">${third}</li>
<li class="option">${fourth}</li>
</ul>
`;
toggleActive();
}
function toggleActive() {
let option = document.querySelectorAll("li.option");
for (let i = 0; i < option.length; i++) {
option[i].onclick = function() {
for (let i = 0; i < option.length; i++) {
if (option[i].classList.contains("active")) {
option[i].classList.remove("active");
}
}
option[i].classList.add("active");
};
}
}
</script>'''