Guess Game Using JavaScript Number Methods

Guess Game Using JavaScript Number Methods

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.

SUBSCRIBE TO OUR NEWSLETTER

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>>        
No alt text provided for this image

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.

No alt text provided for this image


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>>        
No alt text provided for this image

That's how finally we achieved our three goals and guessed a number by implementing Javascript methods and Functions.

To explore more Technical content

SUBSCRIBE TO OUR NEWSLETTER

Praveen S

Software Engineer | React js | React Native | Javascript | Typescript

2 å¹´

Crystal clear explanation ??

要查看或添加评论,请登录

Arun Rajendran的更多文章

  • 10 Powerful LinkedIn Tips To Build An Incredible Profile

    10 Powerful LinkedIn Tips To Build An Incredible Profile

    The professional social networking site LinkedIn was created with business and career-related goals in mind. It offers…

  • Let's Play "Rock", "Paper", or "Scissors" With JavaScript

    Let's Play "Rock", "Paper", or "Scissors" With JavaScript

    Every developer should be familiar with JavaScript because it is one of the three scripting languages used to create…

  • Benefits of Front-End Developer

    Benefits of Front-End Developer

    Front-end developers play a crucial role in web development for several reasons: User Experience: Front-end developers…

  • Do you Wanna Become a Versatile Full-Stack Developer in 2023?

    Do you Wanna Become a Versatile Full-Stack Developer in 2023?

    As we move further into 2023, the demand for full-stack developers continues to grow. Being a full-stack developer…

  • Best Chrome Extensions For Digital marketing In 2022

    Best Chrome Extensions For Digital marketing In 2022

    The Needs and Services of digital marketing continue to be in an increased state and more content at Digital marketers…

    1 条评论
  • HOW TO BECOME AN EFFECTIVE CONTENT WRITER IN 2022

    HOW TO BECOME AN EFFECTIVE CONTENT WRITER IN 2022

    WHO IS A CONTENT WRITER: Content writing is the process of planning, creating, and publishing content for digital…

  • Best digital marketing company in Coimbatore

    Best digital marketing company in Coimbatore

    Are you looking for the Best digital marketing company in Coimbatore, If so, you’ve come to the right place? I have…

    2 条评论
  • Billion Dollar Design - Canva

    Billion Dollar Design - Canva

    In this Article am gonna say about the 2nd richest woman in Australia And she is none other than Melanie Perkins, CEO…

  • JOIN THE REVOLUTION

    JOIN THE REVOLUTION

    Hey people, In this article, I am going to talk about, The very first Ola Electric S1 scooter that has already rolled…

  • LEARN THE MARKETING LAW WHICH MAKES YOU A MILLIONAIRE??

    LEARN THE MARKETING LAW WHICH MAKES YOU A MILLIONAIRE??

    In this article, I am going to talk about The Fundamentals Of Marketing, Digital Marketing Vs Traditional Marketing…

    8 条评论

社区洞察

其他会员也浏览了