Sunday, August 8, 2021

Difference of Two Numbers in JavaScript

 A simple JavaScript program that I wrote that will ask the user to give two numbers and then it will compute the difference of the two numbers and display the result in the screen.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website, kindly contact me also at my email address also. Thank you.

My email address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.





Program Listing

index.htm

<html>
   <title>Difference of Two Numbers in JavaScript</title>
 <style type="text/css">

 body {

  font-familyarial;
  font-weightbold;
  font-size:15px;
   }
 
 form
{
  displayinline-block;
  background-colorlightblue;
  padding6px;
}

label{
  displayblock;
  directionrtl
}

input{
  directionltr
}

label::after{
  contentattr(data-text);
}
 </style>

<script language="JavaScript">

function Solve(){

a = parseInt(document.difference.val_1.value);
b = parseInt(document.difference.val_2.value);

if (a > b) {
  result = a - b;
else {
  result = b - a;
}
document.difference.diff.value = (result);
}

function Clear() {
document.difference.val_1.value ="";
document.difference.val_2.value="";
document.difference.diff.value="";
document.difference.val_1.focus();
 }

</script>
<body>
<br>
<h3>Difference of Two Numbers in JavaScript</h3>
<form name="difference">
<label data-text="First Value">&nbsp;&nbsp; 
<input type="text" name="val_1" autofocus required>
</label>
<br />
<label data-text="Second Value">&nbsp;&nbsp;
<input type="text" name="val_2"required="">
</label>
<br/>
<label data-text="Difference">&nbsp;&nbsp;<input type="text" name="diff" >
</label>
<br /> <br>
<input type="button" value="Convert" title="Click here to solve." onclick="Solve()" />
<input type="button" value="Clear"  
title="Click here to clear the text box."
onclick="Clear()"/>
</form>
</body>
</html>

No comments:

Post a Comment