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>
);
}
}
makeDoors()
returning an array of 3 booleans with exactly one true, and functions likedoesSwitchingWin(doors, choice)
anddoesStayingWin(doors, choice)
. None of these things should be coupled to React. – Jonah Aug 31 '16 at 3:41