• +1 240 877 8161
  • info@thePracticalIT.com
Practical Information Technology
  • Notifi
  • Canvas
    • HOME
    • Start Here
    • Courses
    • EVENTS
      • EVENTS LIST
      • EVENTS GRID
    • BLOG
    • CONTACT
    • IT Courses
  • +1 240 877 8161
  • info@thePracticalIT.com
  • Profile
  • Login | Registration Popup Link
Practical Information Technology
  • Home
  • Courses
    • FEATURED COURSES
      • Web Development
      • Database Development
      • Business Intelligence
    • TRAINING
      • How long does it take?
      • IT Training Methods
      • Hours Per week
      • Payments
    • IT Courses
  • Tips
  • Contact
  • Notifi
  • Start Here

Guess Number from 1 to 10

Homepage - Programming Exercises - Guess Number from 1 to 10

Pract IT

by Practical IT
Posted on 25 Oct, 2021
in
Programming Exercises
0 Comment

You are using going to use the following snippet of code to complete the game we are calling “guess me right”

For this we are using a random method from Math

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Number Guessing.</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <h2 class="text-center">Let's see if you can guess me right.....</h2>
    <form>
        <div class="mb-3">
          <label for="myNumber" class="form-label">Guess Number</label>
          <input type="number" class="form-control" id="myNumber" aria-describedby="guessed number">
        </div>
        <button type="button" class="btn btn-primary">Go!</button>
        <!-- If the user guessed it correct or missed it, it will be reported here.-->
        <div id="message"></div>
      </form>
    </div>
    <script>
        /*
            Using Math.random() you can get a number to be generated between
            1 and given number 
            Using the example from mozilla.org, the function provides the 
            the random number with in the given range.
            Math.random() * 10, for example provides numbers between 1 and 10
         */
        function getRandomInt(max) {
            return Math.floor(Math.random() * max);
        }

        let guessedNumber = document.getElementById('myNumber');
        let button = document.querySelector('button');
        let message = document.querySelector('#message');

        button.addEventListener('click', function(){
            //this is the number to be guessed.
            let generatedNumber = getRandomInt(10); 
            
            //your code continues here.
            //verify if the generated and guessed numbers are equal
            //then update the message accordingly.
            //Bonus, change the colors of the message accordingly. 
        })
    </script>
</body>
</html>

Complete the code to make print the right message of “Yaay.. you guessed it right” when the number is guessed correctly or to print, “Umm.. you missed it, the correct number was [NUMBER]” when the user misses the number.

Tags: exercise game guessing math random number programming
User Avatar
Article by Practical IT

Works at Practical IT. Passionate about Technologies, Business and News.

Previous Story
Full Stack Developer Orientation
Next Story
Full Stack Dev Feb 28 Class

Related Articles

May 2025 IT Class Application

[wpforms id="3116"]

success

Accelerate Your Career with In-Demand IT Training.

Register @ https://register.thepracticalit.com and claim your seat. What is going...

Leave your comment Cancel Reply

(will not be shared)

SearchLeave your keyword

Change your life through Information Technology career today. Thousands have done it – it is your turn now. All you need is a passion to change your life and provide better for yourself and your family. Practical IT takes the rest.

QUICK LINKS

  • Home
  • Courses
    • FEATURED COURSES
      • Web Development
      • Database Development
      • Business Intelligence
    • TRAINING
      • How long does it take?
      • IT Training Methods
      • Hours Per week
      • Payments
    • IT Courses
  • Tips
  • Contact
  • Notifi
  • Start Here

SUBSCRIBE EMAIL

Subscribe to our email list and get tips on IT jobs, career changes openings, seminars and more. We can also SMS you

GET IN TOUCH

You can call us, email us or even better visit us in our office at the heart of Silver Spring. Our Email - info@thePracticalIT.com

+1 240 877 8161

911 Silver Spring Ave #101 Silver Spring MD 20910

Practical IT - Copyright 2023.
  • Sign in

or
  • Login with Facebook
  • Login with Google

Forgot your password?

Lost your password? Please enter your email address. You will receive mail with link to set new password.

Back to login