/    Sign up×
Community /Pin to ProfileBookmark

Javascript Quiz

https://codepen.io/xfvelocity/pen/ZPRKYY

I am trying to make a quiz where you click the checkbox to choose your answer and depending on whether you are correct or not it will change colour. It also needs to show which one they chose by leaving it checked but disable the other checkboxes so that the answer can’t be changed.

Problems: my if statement checks if the correctAnswer is checked and if it is its classed as correct, but if the correct answer is checked on the first question when it loops it thinks the second question is correct

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@rootMar 19.2019 — If you have issues with code, please post it in the thread.

These code repositories are fine but do not adequately illustrate the problem, if someone following in your footsteps finds this thread and then goes to the codepen, I bet its correct... No way of easy way of seeing a before or after in a thread by going back and forth on different pages, etc.
Copy linkTweet thisAlerts:
@xfvelocityauthorMar 19.2019 — const questionContainer = document.querySelector('.questionContainer');
const question = questionContainer.getElementsByClassName('question');
const correctAnswer = questionContainer.querySelector('.correct');

const transition = (area) => {
area.style.transition = '.7s ease';
}

for (let i = 0; i < question.length; i +=1) {
const checkbox = question[i].querySelectorAll('input');
question[i].addEventListener('click', (e) => {
if (e.target.tagName == 'INPUT') {
for (let i = 0; i < checkbox.length; i += 1){
checkbox[i].disabled = 'true';
console.log('Loop');
}
if (correctAnswer.checked) {
question[i].style.backgroundColor = '#63FF76';
transition(question[i]);
console.log('Correct');
} else {
question[i].style.backgroundColor = '#FF6B5A';
transition(question[i]);
console.log('Incorrect');
}
}
});
}
Copy linkTweet thisAlerts:
@rootMar 19.2019 — There are </code></span></C> and <C><span><code> tags you wrap your code in, it helps as you can see with the edit I made.
Copy linkTweet thisAlerts:
@VITSUSAMar 20.2019 — @root#1601960 Root, you are right, i agree with you.
×

Success!

Help @xfvelocity spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...