• +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

DOM – Randomizer, Click, keyup events

Homepage - Programming Exercises - DOM - Randomizer, Click, keyup events

Pract IT

by Practical IT
Posted on 01 Jun, 2021
in
Programming Exercises
0 Comment

On the following snippet of code, you will find a randomizer that populates the input box with numbers. Also, after you add the numbers, when you press the key ‘a’, it shows the summation of the numbers

First see and understand how the addition works. Click on the buttons and populate the input boxes with numbers. Then press ‘a’ on your keyboard to see the summation of the numbers.

The exercise is to work on subtraction when ‘s’ is pressed and for multiplication when ‘m’ is pressed.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Randomizer</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <p>
            Here is a simple randomizer.
        </p>
        <p>
            Click on the populate buttons first to populate the inputboxes with random numbers
        </p>
        <p>
            Then pressing 'a' from your keyboard will show the summation of the random numbers that are generated.
        </p>
        <p>Things to note from this assignment</p>
        <ul>
            <li>How to use randomizar</li>
            <li>How to use switch control</li>
            <li>How to use Key Event</li>
            <li>How to use key code to identify what has been selected</li>
        </ul>
        <p>Assignment</p>
        <p>
            Allow the user to press 's' from the keyboard to show the subtraction of data1 and data2 and when 'm' then show
            the mutiplication of data1 and data2
        </p>
        <form>
            <div class="form-group">
              <label for="data1">Number 1</label>
              <input type="text" class="form-control" id="data1">
              <button id="generate1" type="button" class="btn-sm btn-secondary">Populate</button>
            </div>
            <div class="form-group">
              <label for="data2">Number 2</label>
              <input type="text" class="form-control" id="data2">
              <button id="generate2" type="button" class="btn-sm btn-secondary">Populate</button>
            </div>            
            <div id="result"></div>
          </form>
    </div>
    <script>
        //get the elements
        let generate1 = document.querySelector('#generate1');
        let data1 = document.querySelector('#data1');
        let generate2 = document.querySelector('#generate2');
        let data2 = document.querySelector('#data2');
        let result = document.querySelector('#result');
        
        //handle the generate buttons.
        generate1.addEventListener('click', function(){
            data1.value = randomNumber();
        });

        generate2.addEventListener('click', function(){
            data2.value = randomNumber();
        });
        /*
         * This handles which key the user has pressed to see the result 
         * see https://www.w3.org/2002/09/tests/keys.html for more to see how each key code is mapped to number
         */
        document.querySelector('body').addEventListener('keyup', function(event){
            switch(event.keyCode) {
                case 65:
                case 97:
                    // the user pressed 'a' meaning the user needs to see addition
                    result.innerHTML = "Summation of the two numbers is " + (parseInt(data1.value) + parseInt(data2.value));
                    break;
                case 83:
                case 115:
                    // 's' is clicked so we have subtraction  
                    // handle the subtraction case when 's' is pressed
 
                //case ??: search for keycode for capital and small m
                //case ??:
                    // 'm' is pressed so we will get the product
                    //handle the multiplication case when 'm' is pressed
            }
        });
        //function to create random numbers
        function randomNumber() {
            return Math.floor(Math.random() * 100);
        }
    </script>
</body>
</html>
 
 

What to get out of it

By going through this exercise, you will see how click and keyboard events are handled, how random number generation work and utilize it in the simple challenge like situation.

Also you will see the simple utilization of bootstrap and creating clean pages without much of a hustle.

Show Answer

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Randomizer</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <p>
            Here is a simple randomizer.
        </p>
        <p>
            Click on the populate buttons first to populate the inputboxes with random numbers
        </p>
        <p>
            Then pressing 'a' from your keyboard will show the summation of the random numbers that are generated.
        </p>
        <p>Things to note from this assignment</p>
        <ul>
            <li>How to use randomizar</li>
            <li>How to use switch control</li>
            <li>How to use Key Event</li>
            <li>How to use key code to identify what has been selected</li>
        </ul>
        <p>Assignment</p>
        <p>
            Allow the user to press 's' from the keyboard to show the subtraction of data1 and data2 and when 'm' then show
            the mutiplication of data1 and data2
        </p>
        <form>
            <div class="form-group">
              <label for="data1">Number 1</label>
              <input type="text" class="form-control" id="data1">
              <button id="generate1" type="button" class="btn-sm btn-secondary">Populate</button>
            </div>
            <div class="form-group">
              <label for="data2">Number 2</label>
              <input type="text" class="form-control" id="data2">
              <button id="generate2" type="button" class="btn-sm btn-secondary">Populate</button>
            </div>            
            <div id="result"></div>
          </form>
    </div>
    <script>
        //get the elements
        let generate1 = document.querySelector('#generate1');
        let data1 = document.querySelector('#data1');
        let generate2 = document.querySelector('#generate2');
        let data2 = document.querySelector('#data2');
        let result = document.querySelector('#result');
        
        //handle the generate buttons.
        generate1.addEventListener('click', function(){
            data1.value = randomNumber();
        });
 
        generate2.addEventListener('click', function(){
            data2.value = randomNumber();
        });
        /*
         * This handles which key the user has pressed to see the result 
         * see https://www.w3.org/2002/09/tests/keys.html for more to see how each key code is mapped to number
         */
        document.querySelector('body').addEventListener('keyup', function(event){
            switch(event.keyCode) {
                case 65:
                case 97:
                    // the user pressed 'a' meaning the user needs to see addition
                    result.innerHTML = "Summation of the two numbers is " + (parseInt(data1.value) + parseInt(data2.value));
                    break;
                case 83:
                case 115:
                    // 's' is clicked so we have subtraction  
                    // handle the subtraction case when 's' is pressed
                    result.innerHTML = "subtraction of the two numbers is " + (parseInt(data1.value) / parseInt(data2.value));
                    break;
                case 77: 
                case 109:
                    // 'm' is pressed so we will get the product
                    //handle the multiplication case when 'm' is pressed
                    result.innerHTML = "multiplication of the two numbers is " + (parseInt(data1.value) * parseInt(data2.value));
                    break;
            }
        });
        //function to create random numbers
        function randomNumber() {
            return Math.floor(Math.random() * 100);
        }
    </script>
</body>
</html>

Tags: click eventhandler events handlers javascript keyup randomizer vanilla javascript
User Avatar
Article by Practical IT

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

Previous Story
Function to Return Summation of Odd Numbers
Next Story
Change Background Color of body Every 5 Second

Related Articles

Your Path to a Lucrative IT Career Starts Here – Enroll Now!

Ready to Unlock a High-Paying Career in Tech? Claim Your...

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