Guess Game Using JavaScript Number Methods
Arun Rajendran
Social Media Marketer at Kuku FM | YouTube Growth Strategist | Digital Development Advocate. Let's collaborate to enhance your digital footprint!
Hey, Connections, I would like to start my journey towards excellence in MERN STACK Development, Starting off learning basic concepts in Javascript, I coded a small code which briefs the concepts of guessing a number by using concepts like JavaScript variables and number methods in it.
My task is to make the user guess a number and says whether the number the user guessed is correct or it is lower or it's higher, To achieve this we gonna start working on Js concepts.
I had divided it into several parts
- Getting User Name.
- Use Number Methods to round the random numbers.
- To find whether the user-guessed number is high, low or correct.
To get the final result by implementing all the functionalities in one code, Start by Entering the name of the user
Generally how the?prompt method displays a dialog box that prompts the user for input and it returns the input value if the user clicks "OK", otherwise it returns?null.
<html
? ? <head>
? ? ? ? <title> Guess Game</title>
? ? </head>
? ? <body>
? ? ? ? <script>
? ? ? ? var name =prompt("Enter Your Name" )
? ? ? ? console.log(' Welcome to the Guess Game ' + name)
? ? ? ?
? ? ? ? </script>
? ? </body>
</html>>
To achieve our next set of tasks, we use a Javascript number method Math.random() to get a random number generated.
And multiplying it into 100 with the random number we got initially (Math.random() * 100)
By using a Math.round() method is used to round the random decimal number into a round number format.
领英推è
EXAMPLE:
<html
? ? <head>
? ? ? ? <title> Guess Game</title>
? ? </head>
? ? <body>
? ? ? ? <script>
? ? ? ? ? ? var name = prompt(" Enter your name")
? ? ? ? ? ? console.log(' Welcome to the guess game ' + ?name )
? ? ? ? ? ? var targetNumber = Math.round(Math.random() * 100)
? ? ? ? ? ? console.log(targetNumber)
? ? ? ? </script>
? ? </body>
</html>
>
The final step is to say whether the user guessed the number correctly or high or low, By using the If and If else functions. Here in this example, I had given the input as 12 the number generated is 6 which is high so the function is called and it displays the output as "Your number is High".
<html
? ? <head>
? ? ? ? <title> Guess Game</title>
? ? </head>
? ? <body>
? ? ? ? <script>
? ? ? ? ? ? var name = prompt(" Enter your name")
? ? ? ? ? ? console.log(' Welcome to the guess game ' + ?name )
? ? ? ? ? ? var targetNumber = Math.round(Math.random() * 100)
? ? ? ? ? ? console.log(targetNumber)
? ? ? ? ? ? var maxGuess =10, playerGuessCount = 0
? ? ? ? ? ? var guess = parseInt (prompt("Guess my number"))
? ? ? ? ? ? //console.log( typeof guess)
? ? ? ? ? ?
? ? ? ? ? ? if (guess < targetNumber) {
? ? ? ? ? ? ? ? console.log('Your guess is low')
? ? ? ? ? ? }
? ? ? ? ? ? else if (guess > targetNumber) {
? ? ? ? ? ? ? ? console.log(' your guess is high')
? ? ? ? ? ? } else if( guess == targetNumber) {
? ? ? ? ? ? ? ? console.log(' Good job' + name + ' Your guess is correct')
? ? ? ? ? ? }
? ? ? ? ? ?
? ? ? ? </script>
? ? </body>
</html>>
That's how finally we achieved our three goals and guessed a number by implementing Javascript methods and Functions.
Software Engineer | React js | React Native | Javascript | Typescript
2 å¹´Crystal clear explanation ??