import React from "react"; import ReactDOM from "react-dom"; import "./index.css"; function shuffleArray(array) { /* https://stackoverflow.com/a/12646864 */ for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } } class Decision { constructor(indexA, indexB, choices) { this.indexA = indexA; this.indexB = indexB; this.choiceA = choices[indexA]; this.choiceB = choices[indexB]; } static createDecisions(choices) { let decisions = []; for (var i = 0; i < choices.length; i++) { for (var j = 0; j < choices.length; j++) { if (i !== j) { let d = new Decision(i, j, choices); decisions.push(d); } } } shuffleArray(decisions); return decisions; } } class Options extends React.Component { constructor(props) { super(props); this.state = { choicesText: null, choices: [], decisions: null, decisionsMade: null, currentDecision: null, }; } startChoosing() { this.setState({ decisions: Decision.createDecisions(this.state.choices), decisionsMade: [], currentDecision: 0, }); } choicesTextToChoices(choicesText) { let choices = choicesText.split("\n"); choices = choices.filter((choice) => choice.length > 0); return choices; } choicesOnChange(event) { let choicesText = event.target.value; let choices = this.choicesTextToChoices(choicesText); this.setState({ choicesText: choicesText, choices: choices, }); } makeChoice(i) { this.setState({ currentDecision: this.state.currentDecision + 1, decisionsMade: this.state.decisionsMade.concat([i]), }); } renderChoosing() { const current = this.state.currentDecision; const decision = this.state.decisions[current]; return (
{decision.choiceA}
{decision.choiceB}
| Choice | Count |
|---|