ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Calculations using Javascripts functions and HTML Forms

Updated on March 1, 2013

Java Scripts Calculations

  1. Using appropriate HTML formatting tools such as notepad,notepad++,dreamweaver, Netbeans, visual studio, C# create a file called calculations.html and save it in an appropriate loaction.

<html>
<head><title>Financing</title></head>
<body>
<script language="JavaScript">
function add(){

var x;
var a=document.getElementById('one');
var b=document.getElementById('two');
var c=document.getElementById('ans');
var d=document.getElementById('g');

c.value=0;
c.value=parseInt(c.value);
c.value=parseInt(a.value)+parseInt(b.value)
if (c.value>=50){
d.value="Pass";
}
else{
d.value="Fail";

}

}
</script>

<form name="frmf">
Enter Value 1<input type="text" id="one"><p>
Enter Value 2<input type="text" id="two"><p>
Total is<input type="text" id="ans"><p>
Departent<select id ="cc" name="course">
<option>Finance</option>
<option>Production</option>
<option>ICT</option>
<option>Marketing</option>
</select>
<p>
Grade<input type="text" id="g"><p>
<input type ="button" value="Calculate" onClick="add();">

</form>


</body>

</html>

2.Open the file using appropriate browser such as Google Chrome,internet Explorer,Opera, Safari etc

3.Enter the values for values one and two and click the calculate button.


4.The grading is pass if the total is greater than 50% else the grade is Fail.

Screen shot

Example two : Exam Analysis

<html>
<head>
<title>School Exams</title>
<style type="text/css">
td{border;6px solid #CCC}
table{
background-color:silver;
border:1px solid black;
}
body{
background-color:#008080;
}
h1{color:red;}
</style>
</head>

<body>

<script Language="JavaScript">
function calculate()
{
var bio=parseInt(document.frmexamanalysis.BIO.value);
var eng=parseInt(document.frmexamanalysis.English.value);
var phy=parseInt(document.frmexamanalysis.Physics.value);
var his=parseInt(document.frmexamanalysis.History.value);
var ttl=(bio+eng+phy+his)
var ave=parseFloat(ttl/4)
document.frmexamanalysis.Totalmarks.value=ttl;
document.frmexamanalysis.Average.value=ave;
if (ave>=80)
{
document.frmexamanalysis.Grade.value="A";
}
else if(ave>=60)
{
document.frmexamanalysis.Grade.value="B";
}
else if(ave>=40)
{
document.frmexamanalysis.Grade.value="C";
}
else
{
document.frmexamanalysis.Grade.value="D";
}
}


</script>
<h1><center>High school Exam analysis</center></h1>
<form name="frmexamanalysis">
<table width="50%" height="50%" border="0" align="center">
<tr><td><p>Name<input type="text" id="name" name="Name"></p></td>
<td><p>AdmNo&nbsp&nbsp&nbsp<input type="text" id="adm" name="AdmNo"></p></td></tr>
<tr><td><p>BIO<input type="text" id="bio" name="BIO"></p></td>
<td><p>English<input type="text" id="eng" name="English"></p></td></tr>
<tr><td><p>Physics<input type="text" id="phy" name="Physics"></p></td>
<td><p>History<input type="text" id="his" name="History"></p></td></tr>
<tr><td><p>Total marks<input type="text" id="ttl" name="Totalmarks"></p></td>
<td><p>Average<input type="text" id="ave" name="Average"></p></td></tr>
<tr><td><p>Grade<input type="text" id="grd" name="Grade"></p></td></tr>

<tr>
<td><input type="Button" value="CALCULATE" OnClick="calculate()";></td>
<td><button type="RESET" value="Refresh">REFRESH</button></td>
</tr>

</table>
</form>
</body>
</html>

ExamAnalysis

Exam Analysis Class assignment
Exam Analysis Class assignment | Source

Example Three: Payroll

<html>
<head>
<title>Payroll</title>
</head>
<style type="text/css">
td{border;6px solid #CCC}
table{
background-color:aqua;
border:1px solid black}
body{
background-color:gray;
background-image:url(alicia.jpg);
background-repeat:no-repeat;
background-position:top left;
}
</style>
<body>
<script Language="JavaScript">
function calculate()
{
var bp=parseInt(document.frmpayroll.bp.value);
var paye=0.08*bp
var ha=0.1*bp
var nssf=0.01*bp
var nhif=0.02*bp
var gp=bp+ha
var np=gp-(nssf+nhif+paye)
document.frmpayroll.HA.value=ha;
document.frmpayroll.NSSF.value=nssf;
document.frmpayroll.NHIF.value=nhif;
document.frmpayroll.grosspay.value=gp;
document.frmpayroll.netpay.value=np;
document.frmpayroll.paye.value=paye;
}
</script>
<h1><center>Employees Payroll Example</center></h1>
<form name="frmpayroll">
<table width="50%" height="50%" border="0" align="center">
<tr><td colspan="2"><p>Name<input type="text" id="name" name="Name"></p></td></tr>
<tr><td><p>Basic Pay<input type="text" id="bp" name="bp"></p></td>
<td><p>Department:
<select name ="Department" id="dep">
<option>Production</option>
<option>Finance</option>
<option>Human Resources</option>
<option>Marketing</option>
</select>
</p></td></tr>
<tr><td><p>NHIF<input type="text" id="nhif" name="NHIF"></p></td>
<td><p>House Allowance <input type="text" id="ha" name="HA"></p></td></tr>
<tr><td><p>NSSF<input type="text" id="nssf" name="NSSF"></p></td>
<td><p>PAYE<input type="text" id="paye" name="paye"></p></td></tr>
<tr><td><p>GROSS PAY<input type="text" id="gp" name="grosspay"></p></td>
<td><p>NET PAY<input type="text" id="np" name="netpay"></p></td>
</tr>


<tr>
<td colspan="1"><input type="Button" value="CALCULATE" OnClick="calculate()";</td>
<td ><button type="RESET" value="CLEAR">clear</button></td>
</tr>
</table>
</form>
</body>
</html>

Payroll screenshot

Calculations using CSS,HTML and JavaScript exam Analysis and Payroll Examples

    0 of 8192 characters used
    Post Comment

    • profile image

      naomi binsari 

      3 years ago

      thank you sir, you have really helped me to like programming.

    • nnjiru profile imageAUTHOR

      nnjiru 

      4 years ago

      Chris Los you use parseFloat instead of parseInt

    • profile image

      chris los 

      5 years ago

      By example two,when you fill in 4.5 6.5 7.5 8.5= 25

      I am missing behind . .5 .5 .5 .5 in de calculation?

      PLEASE TELL ME WHAT I HAVE TO DO.

    • profile image

      luke 

      5 years ago

      thank you dude

    • profile image

      komen 

      5 years ago

      nnjiru please give us more tutorials. You have really helped me for my project

    • nnjiru profile imageAUTHOR

      nnjiru 

      5 years ago

      Mathiang welcome for more

    • profile image

      Mathiang 

      5 years ago

      Very nice tutorial.It has solved my headache

    • nnjiru profile imageAUTHOR

      nnjiru 

      5 years ago

      This is a cool tutorial. awesome

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)