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.
<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>
Comments
Leave a comment