Ayoo,
Since learning functional programming I’ve been stumped about how to properly handle nested logic cases. Take the following example: I’m coding a quiz. After the user clicks a possible answer, I update the elements to add classes indicating correctness:
// While looping over possible answers
let className = "question__answer " ++ (switch (selectedAnswer) {
// If no answer has been selected yet, we don't need to add extra classes
| None => ""
| Some(selectedAnswerValue) => {
// If this is the correct answer, add the corresponding class
if (answer.correct) {
"question__answer_correct"
} else {
// Only add the "failure" class if this was the selected answer
if (selectedAnswerValue.text == answer.text) {
"question__answer_incorrect"
} else {
""
}
}
}
})
I know I’m doing it wrong. This is anything but concise and clear code and the compiler says there’s a syntax error. What is the alternative?
Thanks!