diff --git a/src/GuessControl.jsx b/src/GuessControl.jsx index 64108b5..f46c10a 100644 --- a/src/GuessControl.jsx +++ b/src/GuessControl.jsx @@ -1,47 +1,69 @@ -import React, { Component } from "react"; +import React, { useState } from "react"; import Button from "./Button"; -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 ( -
- - -
- ); - } +function GuessControl({ onGuess }) { + const [currentGuess, setCurrentGuess] = useState(""); + + //step 4 create a handleInputChange function + const handleInputChange = (event) => { + setCurrentGuess(event.target.value); + }; + + //step 5 create onsubmitGuess function + const onSubmitGuess = () => { + onGuess(Number(currentGuess)); + setCurrentGuess(""); //Set the onChange property for the input element + }; + + return ( +
+ + +
+ ); } export default GuessControl; + +//import React, { Component } from "react"; +// 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 ( +//
+// +// +//
+// ); + +//step 3 create a new function diff --git a/src/NumberGuessingGame.jsx b/src/NumberGuessingGame.jsx index 07c5a24..f383c2c 100644 --- a/src/NumberGuessingGame.jsx +++ b/src/NumberGuessingGame.jsx @@ -1,77 +1,125 @@ -import React, { Component } from "react"; +//import React, { Component } from "react"; +import React, { useState } from "react"; import GuessControl from "./GuessControl"; import GuessMessage from "./GuessMessage"; import GameOver from "./GameOver"; +const getRandomNumber = () => Math.floor(Math.random() * 100) + 1; +const MAX_ATTEMPTS = 5; + /** * * Returns a random integer number from 1-100 inclusive */ -function getRandomNumber() { - return Math.floor(Math.random() * 100) + 1; -} -const MAX_ATTEMPTS = 5; +function NumberGuessingGame() { + const [numberToGuess, setNumberToGuess] = useState(getRandomNumber()); + const [numberOfGuesses, setNumberOfGuesses] = useState(0); + const [latestGuess, setLatestGuess] = useState(null); + + const isCorrectGuess = latestGuess === numberToGuess; -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; - - const isGameOver = - isCorrectGuess || this.state.numberOfGuesses === MAX_ATTEMPTS; - - return ( -
-

I'm thinking of a number from 1 to 100.

-

- Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries? -

- - {isGameOver && ( - - )} - {!isGameOver && ( - - )} -
- ); - } + const isGameOver = isCorrectGuess || numberOfGuesses >= MAX_ATTEMPTS; + + //Create a handleInputChange function + const handleGuess = (guess) => { + setLatestGuess(Number(guess)); + setNumberOfGuesses((prev) => prev + 1); + }; + + //create a handleReset function + const handleReset = () => { + setNumberToGuess(Math.floor(Math.random() * 100) + 1); + setNumberOfGuesses(0); + setLatestGuess(null); + }; + + return ( +
+

I'm thinking of a number from 1 to 100.

+

+ Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries? +

+ + + {isGameOver ? ( + + ) : ( + + )} +
+ ); } export default NumberGuessingGame; + +//function getRandomNumberOld() { +// return Math.floor(Math.random() * 100) + 1; +// } + +// const MAX_ATTEMPTS = 5; + +// class NumberGuessingGame extends Component { +// constructor(props) { +// super(props); + +// this.state = { +// numberToGuess: getRandomNumberOld(), +// 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: getRandomNumberOld(), +// numberOfGuesses: 0, +// latestGuess: null, +// }); +// } + +// render() { +// const isCorrectGuess = this.state.latestGuess === this.state.numberToGuess; + +// const isGameOver = +// isCorrectGuess || this.state.numberOfGuesses === MAX_ATTEMPTS; + +// return ( +//
+//

I'm thinking of a number from 1 to 100.

+//

+// Can you guess the number I am thinking of in {MAX_ATTEMPTS} tries? +//

+// +// {isGameOver && ( +// +// )} +// {!isGameOver && ( +// +// )} +//
+// ); +// } +// }