I am trying to create a quiz system where selecting different options will result in different outputs, however, the output is always rendered the same. What's the issue?
A user chooses between button 1 and 2, and then 3 and 4. He makes two choices which sets a particular state for this scenario. Eg. choosing 1 and 3 results in ['a','c']. When the output button is pressed, it will check against all the option results and console.log the output for the specific scenario.
import React from "react";
import ReactDOM from "react-dom";
import stylesheet from "./stylesheet.css";
class App extends React.Component {
constructor(props) {
super(props);
this.state = { myArray: [] };
}
firstHandler = () => {
this.setState(previousState => ({
myArray: [...previousState.myArray, "a"]
}));
};
secondHandler = () => {
this.setState(previousState => ({
myArray: [...previousState.myArray, "b"]
}));
};
thirdHandler = () => {
this.setState(previousState => ({
myArray: [...previousState.myArray, "c"]
}));
};
fourthHandler = () => {
this.setState(previousState => ({
myArray: [...previousState.myArray, "d"]
}));
};
output = () => {
const ar1 = [];
if (this.state.myArray.values === ar1.values) {
return console.log("Death");
} else if (this.state.myArray.values === ["a", "b"].values) {
return console.log("Life");
} else if (this.state.myArray.values === ["a", "d"].values) {
return console.log("Wealth");
} else {
return console.log("Dispair");
}
};
render() {
return (
<div>
<div className="row-one">
<button className="option-one" onClick={this.firstHandler}>
button 1
</button>
<button className="option-two" onClick={this.secondHandler}>
button 2
</button>
</div>
<div className="row-two">
<button className="option-three" onClick={this.thirdHandler}>
button 1
</button>
<button className="option-four" onClick={this.fourthHandler}>
button 2
</button>
</div>
<div>{this.state.myArray}</div>
<button onClick={this.output}>...</button>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
Keeps on outputting 'Death' regardless of the options chosen