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}

); } renderButton() { let choices = this.state.choices; let button = null; if (choices.length > 1 && choices[1]) { button = ( ); } else { button = ( ); } return button; } renderChoiceInputField() { return (

Type or paste your choices

this.onSubmit}>
); } renderCurrentChoices() { let choices = this.state.choices; let choicesToRender = choices.map((choice, index) => { return
  • {choice}
  • ; }); let button = this.renderButton(); return (

    Your choices

      {choicesToRender}
    {button}
    ); } renderSpecifyChoices() { let input = this.renderChoiceInputField(); let output = this.renderCurrentChoices(); return (
    {input} {output}
    ); } renderResult() { let choices = this.state.choices.map((choice, index) => { return { choice: choice, count: 0, }; }); for (const i of this.state.decisionsMade) { if (i !== -1) { choices[i].count += 1; } } choices.sort((a, b) => b.count - a.count); let choicesToRender = choices.map((choice, index) => { return ( {choice.choice} {choice.count} ); }); return (
    {choicesToRender}
    Choice Count
    ); } render() { if (!this.state.decisions) { return this.renderSpecifyChoices(); } else if (this.state.currentDecision < this.state.decisions.length) { return this.renderChoosing(); } else { return this.renderResult(); } } } ReactDOM.render(, document.getElementById("root"));