-
-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db1503c
commit 4e3fb64
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { useState, FormEvent } from 'react'; | ||
import { block } from 'million/react'; | ||
interface NumberGuessingGameProps {} | ||
|
||
const NumberGuessingGame: React.FC<NumberGuessingGameProps> = block(() => { | ||
const [systemNumber] = useState(() => Math.floor(Math.random() * 10000) + 1); | ||
const [userNumber, setUserNumber] = useState<number>(0); | ||
const [attempts, setAttempts] = useState<number>(0); | ||
const [message, setMessage] = useState<string>( | ||
'Guess the number between 1 and 10000!', | ||
); | ||
|
||
const handleGuess = (e: FormEvent) => { | ||
e.preventDefault(); | ||
if (userNumber === systemNumber) { | ||
setMessage(`Congratulations, you win in ${attempts} moves!`); | ||
} else { | ||
setAttempts(attempts + 1); | ||
setMessage( | ||
userNumber > systemNumber | ||
? 'Please decrease your value' | ||
: 'Please increase your value', | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h1>Number Guessing Game</h1> | ||
<p>{message}</p> | ||
<p>Moves taken: {attempts}</p> | ||
<form onSubmit={handleGuess}> | ||
<input | ||
type="number" | ||
value={userNumber} | ||
onChange={(e) => setUserNumber(parseInt(e.target.value))} | ||
/> | ||
<button type="submit">Guess</button> | ||
</form> | ||
<h3> | ||
Fun Fact : This game will take max 14 moves to win . Find out how :) | ||
</h3> | ||
</div> | ||
); | ||
}); | ||
|
||
export default NumberGuessingGame; |
4e3fb64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sink – ./packages/kitchen-sink
sink-git-main-millionjs.vercel.app
sink.million.dev
million-kitchen-sink-atit.vercel.app
sink-millionjs.vercel.app
4e3fb64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
million-kitchen-sink – ./packages/kitchen-sink
million-kitchen-sink-millionjs.vercel.app
million-kitchen-sink-git-main-millionjs.vercel.app
million-kitchen-sink.vercel.app