I’m doing a PWA quiz application using React.js and I’ve met the following problematic:
I can get questions objects with only one answer, and some with multiple.
In the case there is only one possible answer, I want to force the user to only have one possibility.
To do that, I made the following algorithm:
JavaScript
x
14
14
1
clickOnChoice = (key) => {
2
if (this.state && this.state.correctAnswers) {
3
let newChoices = INITIAL_CHOICES; // {}
4
if (this.state.multiChoice) {
5
console.log("this.state.multiChoice:", this.state.multiChoice); // this.state.multiChoice: false ???
6
newChoices = JSON.parse(JSON.stringify(this.state.choices)); // {answer_b: 1}
7
}
8
newChoices[key] = 1 - (newChoices[key] | 0); // {answer_b: 1, answer_a: 1}
9
this.setState({
10
choices: newChoices
11
}, this.updateNextButtonState);
12
}
13
}
14
However the execution seems to ignore the condition if (this.state.multiChoice)
.
What am I missing?
Maybe I need a cup of coffee… ☕
Anyway, thanks in advance!
Advertisement
Answer
It is more than likely you are trying to checking a string of ‘false’ rather than an actual boolean value.
you can check that the string is the expected boolean if (this.state.multiChoice === 'true')
or change the value of the state property to true || false