function checkAnswers() {
let score = 0;
if (options1.value == 'correct') {
score++;
options1.style.backgroundColor = 'lightGreen';
} else {
options1.style.backgroundColor = 'pink';
}
if (options2.value == 'correct') {
score++
options2.style.backgroundColor = 'lightGreen';
} else {
options2.style.backgroundColor = 'pink';
}
if (options3.value == 'correct') {
score++
options3.style.backgroundColor = 'lightGreen';
} else {
options3.style.backgroundColor = 'pink';
}
alert(`your score is ${score}`)
}
checkButton.onclick = checkAnswers;
I want to make this script to be ternary, how do I do that? I have tried to put code like this below :
function checkAnswers() {
options1.value == "correct" ? score++;
options1.style.backgroundColor = 'lightGreen': options1.style.backgroundColor = 'pink';
options2.value == "correct" ? score++;
options2.style.backgroundColor = 'lightGreen': options2.style.backgroundColor = 'pink';
options3.value == "correct" ? score++;
options3.style.backgroundColor = 'lightGreen': options3.style.backgroundColor = 'pink';
}
checkButton.onclick = checkAnswers;
Is that possible if we could make the first code on above to be ternary operator, become shorter?
Aucun commentaire:
Enregistrer un commentaire