Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>
  );
  }
}
share|improve this question
1  
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. – mdfst13 Aug 30 '16 at 23:34
2  
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. – Jonah Aug 31 '16 at 3:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.