Answer to Question #261023 in HTML/JavaScript Web Application for claes

Question #261023

Write a JavaScript Program (Using a single HTML and JavaScript file)

to input 2 numbers and determine (using a <p> tag whether:

a. The 2 numbers are equal

b. The first is greater then the second.

c. The second is greater than the first.


Display the message on change of any of the 2 inputs.


1
Expert's answer
2021-11-05T17:27:52-0400
<html>
 <head>
 <title>Average</title>


 <script language="javascript" type="text/javascript">
    
   
     window.onload = function () {
 


         txtNumber1.addEventListener('change', displayResult);
         txtNumber2.addEventListener('change', displayResult);


         function displayResult() {
         
   
            
             let number1=parseInt(txtNumber1.value);
             let number2=parseInt(txtNumber2.value);
             //a. The 2 numbers are equal
             if(number1==number2){
                document.getElementById("result").innerHTML ="The 2 numbers are equal";   
             }
            //b. The first is greater then the second.
            if(number1>number2){
                document.getElementById("result").innerHTML ="The first is greater then the second.";   
             }
            //c. The second is greater than the first.
            if(number1<number2){
                document.getElementById("result").innerHTML ="The second is greater than the first.";   
             }
         };
     }
    </script> 


 </head>
 <body>
 <form>
 
 <table>
  <tr>
  <tr>
    <td>Enter number 1</td>
    <td> <input id="txtNumber1" type="text" value="" ></td>
  </tr>
   <tr>
    <td>Enter number 2</td>
    <td> <input id="txtNumber2" type="text" value=""></td>
  
</table>


 <p id="result"></p>
 </form>
</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS