Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 67 additions & 39 deletions src/GuessControl.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,75 @@
import React, { Component } from "react";
import React from "react";
import Button from "./Button";
function GuessControl({ onGuess }) {
const [currentGuess, setCurrentGuess] = React.useState('');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can import useState from react to prevent from re-writing React

function handleInputChange(event) {
setCurrentGuess(event.target.value);
}
function onSubmitGuess() {
onGuess(currentGuess);
setCurrentGuess('');
}
return (
<div>
<input
type="number"
value={currentGuess}
onChange={handleInputChange} />
<Button onClick={onSubmitGuess}>Submit Guess</Button>
</div>
);
}
export default GuessControl;

class GuessControl extends Component {
constructor(props) {
super(props);

this.state = {
currentGuess: "",
};

/**
* These lines are required to make the methods/functions declared on this
* class have the correct `this` object when they run.
*/
this.handleInputChange = this.handleInputChange.bind(this);
this.onSubmitGuess = this.onSubmitGuess.bind(this);
}

handleInputChange(event) {
this.setState({ currentGuess: event.target.value });
}

onSubmitGuess() {
// Since the values from an HTML input are strings by default,
// convert to a number for the returned guess value
// by passing in the string to the Number function.
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
this.props.onGuess(Number(this.state.currentGuess));
this.setState({ currentGuess: "" });
}

render() {
return (
<div>
<input
type="number"
value={this.state.currentGuess}
onChange={this.handleInputChange}
/>
<Button onClick={this.onSubmitGuess}>Submit Guess</Button>
</div>
);
}
}

export default GuessControl;


// class GuessControlOld extends Component {
// constructor(props) {
// super(props);

// this.state = {
// currentGuess: "",
// };

// /**
// * These lines are required to make the methods/functions declared on this
// * class have the correct `this` object when they run.
// */
// this.handleInputChange = this.handleInputChange.bind(this);
// this.onSubmitGuess = this.onSubmitGuess.bind(this);
// }

// handleInputChange(event) {
// this.setState({ currentGuess: event.target.value });
// }

// onSubmitGuess() {
// // Since the values from an HTML input are strings by default,
// // convert to a number for the returned guess value
// // by passing in the string to the Number function.
// // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
// this.props.onGuess(Number(this.state.currentGuess));
// this.setState({ currentGuess: "" });
// }

// render() {
// return (
// <div>
// <input
// type="number"
// value={this.state.currentGuess}
// onChange={this.handleInputChange}
// />
// <Button onClick={this.onSubmitGuess}>Submit Guess</Button>
// </div>
// );
// }
// }

// export default GuessControl;
137 changes: 87 additions & 50 deletions src/NumberGuessingGame.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,114 @@
import React, { Component } from "react";
import React from "react";
import GuessControl from "./GuessControl";
import GuessMessage from "./GuessMessage";
import GameOver from "./GameOver";

/**
*
* Returns a random integer number from 1-100 inclusive
*/
function getRandomNumber() {
return Math.floor(Math.random() * 100) + 1;
}

const MAX_ATTEMPTS = 5;

class NumberGuessingGame extends Component {
constructor(props) {
super(props);

this.state = {
numberToGuess: getRandomNumber(),
numberOfGuesses: 0,
latestGuess: null,
};

/**
* These lines are required to make the methods/functions declared on this
* class have the correct `this` object when they run.
*/
this.handleGuess = this.handleGuess.bind(this);
this.handleReset = this.handleReset.bind(this);
}

handleGuess(guess) {
this.setState({
latestGuess: guess,
numberOfGuesses: this.state.numberOfGuesses + 1,
});
}

handleReset() {
this.setState({
numberToGuess: getRandomNumber(),
numberOfGuesses: 0,
latestGuess: null,
});
}

render() {
const isCorrectGuess = this.state.latestGuess === this.state.numberToGuess;

function NumberGuessingGame() {
const[numberToGuess, setNumberToGuess]= React.useState(getRandomNumber());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can import useState like mentioned above

const[numberOfGuesses, setNumberOfGuesses]= React.useState(0);
const[latestGuess, setLatestGuess]= React.useState(null);
function handleGuess(guess) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix indentation to match state variable indentation

setLatestGuess(Number(guess))
setNumberOfGuesses(numberOfGuesses + 1)
}
function handleReset() {
setNumberToGuess(getRandomNumber());
setNumberOfGuesses(0);
setLatestGuess(null);
}
const isCorrectGuess = latestGuess === numberToGuess;
const isGameOver =
isCorrectGuess || this.state.numberOfGuesses === MAX_ATTEMPTS;

isCorrectGuess || numberOfGuesses === MAX_ATTEMPTS;
return (
<div>
<h2>I'm thinking of a number from 1 to 100.</h2>
<h2>
Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries?
</h2>
<GuessControl onGuess={this.handleGuess} />
<GuessControl onGuess={handleGuess} />
{isGameOver && (
<GameOver hasWon={isCorrectGuess} onReset={this.handleReset} />
<GameOver hasWon={isCorrectGuess} onReset={handleReset} />
)}
{!isGameOver && (
<GuessMessage
guess={this.state.latestGuess}
numberToGuess={this.state.numberToGuess}
numberOfGuesses={this.state.numberOfGuesses}
guess={latestGuess}
numberToGuess={numberToGuess}
numberOfGuesses={numberOfGuesses}
/>
)}
</div>
);
}
}

}
export default NumberGuessingGame;



// class NumberGuessingGameOld extends Component {
// constructor(props) {
// super(props);

// this.state = {
// numberToGuess: getRandomNumber(),
// numberOfGuesses: 0,
// latestGuess: null,
// };

// /**
// * These lines are required to make the methods/functions declared on this
// * class have the correct `this` object when they run.
// */
// this.handleGuess = this.handleGuess.bind(this);
// this.handleReset = this.handleReset.bind(this);
// }

// handleGuess(guess) {
// this.setState({
// latestGuess: guess,
// numberOfGuesses: this.state.numberOfGuesses + 1,
// });
// }

// handleReset() {
// this.setState({
// numberToGuess: getRandomNumber(),
// numberOfGuesses: 0,
// latestGuess: null,
// });
// }

// render() {
// const isCorrectGuess = this.state.latestGuess === this.state.numberToGuess;

// const isGameOver =
// isCorrectGuess || this.state.numberOfGuesses === MAX_ATTEMPTS;

// return (
// <div>
// <h2>I'm thinking of a number from 1 to 100.</h2>
// <h2>
// Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries?
// </h2>
// <GuessControl onGuess={this.handleGuess} />
// {isGameOver && (
// <GameOver hasWon={isCorrectGuess} onReset={this.handleReset} />
// )}
// {!isGameOver && (
// <GuessMessage
// guess={this.state.latestGuess}
// numberToGuess={this.state.numberToGuess}
// numberOfGuesses={this.state.numberOfGuesses}
// />
// )}
// </div>
// );
// }
// }

// export default NumberGuessingGame;