Friday, September 21, 2007
Creating a simple calc using JavaScript
This program, is to simulates the simple calc of two input numbers, and also tackle all the situations in order to avoid invalid user inputs to the program.Javascript
function is below listed.
function calculate(num,form)
{
a=(form1.a.value) b=(form1.b.value)
if(form1.a.value!="" && form1.b.value!="" )
{
if((isNaN(a)==false)&&(isNaN(b)==false))
{
if(num==1) { c=(a*1)+(b*1) }
if(num==2) { c=a-b }
if(num==3) { c=a*b }
if(num==4) {
if(b!=0){c=a/b }
else {alert("error : divide by zero"); c=" "}
}
form1.ans.value = c
}
else { alert("error : input must be numeric ") }
}
else { alert("error : input fields can't be empty ") }
}
function resetin(form){
form.a.value=""
form.b.value=""
}
function resetout(form){
form.ans.value=""
}
function is below listed.
function calculate(num,form)
{
a=(form1.a.value) b=(form1.b.value)
if(form1.a.value!="" && form1.b.value!="" )
{
if((isNaN(a)==false)&&(isNaN(b)==false))
{
if(num==1) { c=(a*1)+(b*1) }
if(num==2) { c=a-b }
if(num==3) { c=a*b }
if(num==4) {
if(b!=0){c=a/b }
else {alert("error : divide by zero"); c=" "}
}
form1.ans.value = c
}
else { alert("error : input must be numeric ") }
}
else { alert("error : input fields can't be empty ") }
}
function resetin(form){
form.a.value=""
form.b.value=""
}
function resetout(form){
form.ans.value=""
}
Subscribe to:
Posts (Atom)
