1
\$\begingroup\$

Any suggestions for optimization are appreciated!

import React, { Component } from 'react';

export default class Trial extends Component {
  constructor(props) {
    super(props);
    this.state = {
      wins: 0,
      losses: 0,
      false_doors: []
    };
    [...Array(1000000)].forEach(() => {
      this.newTrial();
    });
  }
  newTrial() {
    this.state.choices = this.setupChoices();
    this.state.first_choice = this.chooseRandom(0, 2);
    this.state.door_revealed = this.revealDoor();
    this.state.second_choice = this.secondChoice();
    this.state.second_choice === this.state.correct_door ? this.state.wins++ : this.state.losses++;
  }
  setupChoices() {
    switch (this.chooseRandom(0, 2)) {
      case 0:
        this.state.correct_door = 0;
        this.state.false_doors = [1, 2];
        return [true, false, false];
      case 1:
        this.state.correct_door = 1;
        this.state.false_doors = [0, 2];
        return [false, true, false];
      case 2:
        this.state.correct_door = 2;
        this.state.false_doors = [0, 1];
        return [false, false, true];
    }
  }
  chooseRandom(min, max) {
    return Math.floor(Math.random() * (max-min+1) + min);
  }
  revealDoor() {
    if (this.state.first_choice === this.state.correct_door) {
      return this.state.false_doors[this.chooseRandom(0, 1)];
    } else {
      for ( let door of this.state.false_doors ) {
        if ( door !== this.state.first_choice ) { return door; }
      }
    }
  }
  secondChoice() {
    switch (this.props.type) {
      case 'switcher':
        for ( let door of [0, 1, 2] ) {
          if ( door !== this.state.first_choice && door !== this.state.door_revealed ) {
            return door;
          }
        }
      case 'non-switcher':
        return this.state.first_choice;
    }
  }
  render() {
  return (
    <div className="trial">
      {this.props.type}
      <p>Wins: {this.state.wins}, Losses: {this.state.losses}</p>
    </div>
  );
  }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ It is often helpful to define the problem in English before showing the code. Especially in a case like this, since the Monty Hall problem has several variants. \$\endgroup\$ – mdfst13 Aug 30 '16 at 23:34
  • 2
    \$\begingroup\$ I see from your profile that you're learning fast. The best advice I can give you about this code is that you shouldn't be using react here (at least, not for anything but displaying the results, and even for that it's overkill). You have a pure math problem. Model it with a simple data structures and pure, vanilla js functions. Think about things makeDoors() returning an array of 3 booleans with exactly one true, and functions like doesSwitchingWin(doors, choice) and doesStayingWin(doors, choice). None of these things should be coupled to React. \$\endgroup\$ – Jonah Aug 31 '16 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.