Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code to implement simple calculator in JavaScript: 

<html>

<head>

<title>Function Calculator</title>

<script type="text/javascript">

   function show_cal(){

    function num(){

    a=Number(document.form1.num1.value);  

    b=Number(document.form1.num2.value); 

    c=a+b;

    document.form1.result1.value=c;

    a=Number(document.form1.num1.value);  

    b=Number(document.form1.num2.value); 

    c=a-b;

    document.form1.result2.value=c;

    a=Number(document.form1.num1.value);  

    b=Number(document.form1.num2.value); 

    c=a*b;

    document.form1.result3.value=c;

    a=Number(document.form1.num1.value);  

    b=Number(document.form1.num2.value); 

    c=a/b;

    document.form1.result4.value=c;

    }

    function addition(){

    a=Number(document.form1.num3.value);  

    b=Number(document.form1.num4.value); 

    c=a+b;

    document.form1.result1.value=c;

    }

    function subtraction(){

    a=Number(document.form1.num5.value);  

    b=Number(document.form1.num6.value); 

    c=a-b;

    document.form1.result2.value=c;

    }

    function multiply(){

    a=Number(document.form1.num7.value);  

    b=Number(document.form1.num8.value); 

    c=a*b;

    document.form1.result3.value=c;

    }

    function division(){

    a=Number(document.form1.num9.value);  

    b=Number(document.form1.num10.value); 

    c=a/b;

    document.form1.result4.value=c;

    }

    /*function formValidator(){

    var number = document.getElementById('number');

        if(isNumeric(number, "Only Numbers pls")){

        return true;

        }return false;

    }

    function notEmpty(elem, helperMsg){ //gia keno

        if(elem.value.length == 0){

            alert(helperMsg);

            elem.focus(); // set the focus to this input

        return false;

        }

    return true;

    }

    function show_clear(){

        document.form1.display.value=null;

        num1= null;

        num2 = null;

        lastaction= null;

        action = null;

    } */

}

</script>

</head>

<body>

<table width="400" align="center" bgcolor="#C0C0C0">

    <form name="form1" action="">

    <tr align="center">

    <td width="600" height="112" align="center" border="1">

        <h1 align="center"> Calculator </h1>

    Number 1: <input name="num1" type="text" size=10/>

    Number 2: <input name="num2" type="text" size=10/>

        </td>

    </tr>

    <tr align="center">

    <td width="500">

    <input name="num3" type="text" size=10/> + 

    <input name="num4" type="text" size=10/> =

    <input name="result1" type="text" size=10/>

    </td>

    </tr>

    <br/>

    <tr align="center">

    <td width="500">

    <input name="num5" type="text" size=10/> -

    <input name="num6" type="text" size=10/> =

    <input name="result2" type="text" size=10/>

    </td>

    </tr>

    <br/>

    <tr align="center">

    <td width="500">

    <input name="num7" type="text" size=10/> *

    <input name="num8" type="text" size=10/> =

    <input name="result3" type="text" size=10/>

    </td>

    </tr>

    <br/>

    <tr align="center">

    <td width="500">

    <input name="num9" type="text" size=10/> /

    <input name="num10" type="text"size=10/> =

    <input name="result4" type="text" size=10/>

    </td>

    </tr>


 

        <br/>

    <td height="13"></tr>

    <tr align="center" width="100">

    <td>

    <input name="result" type="button" onClick="show_cal()" value="Result" />

    <input type="button" onClick="show_clear()" value="Clear"/>

    </td>

    </tr>

</form>

</table>

</body>

</html>

The expected output: 

When a user enters "Number 1" and "Number 2", the following should work.

Number1 = 5, Number2 = 3

then   =>

5 + 3 = 8,

5 - 3 = 2,

5 * 3 = 15,

5 / 3 = 1.6

Can anyone tell me what I’m doing wrong?

1 Answer

0 votes
by (19.7k points)

You need to call the function ‘sum’ which is within the ‘function show_calc’ and to call the function ‘num’ when you do the ‘showcalc’ function. 

Check the code below:

<script type="text/javascript">

   function show_cal(){

    function num(){

      a=Number(document.form1.num1.value);  

      b=Number(document.form1.num2.value); 

      c=a+b;

      document.form1.result1.value=c;

      a=Number(document.form1.num1.value);  

      b=Number(document.form1.num2.value); 

      c=a-b;

      document.form1.result2.value=c;

      a=Number(document.form1.num1.value);  

      b=Number(document.form1.num2.value); 

      c=a*b;

      document.form1.result3.value=c;

      a=Number(document.form1.num1.value);  

      b=Number(document.form1.num2.value); 

      c=a/b;

      document.form1.result4.value=c;

    }

    function addition(){

      a=Number(document.form1.num3.value);  

      b=Number(document.form1.num4.value); 

      c=a+b;

      document.form1.result1.value=c;

    }

    function subtraction(){

      a=Number(document.form1.num5.value);  

      b=Number(document.form1.num6.value); 

      c=a-b;

      document.form1.result2.value=c;

    }

    function multiply(){

      a=Number(document.form1.num7.value);  

      b=Number(document.form1.num8.value); 

      c=a*b;

      document.form1.result3.value=c;

    }

    function division(){

      a=Number(document.form1.num9.value);  

      b=Number(document.form1.num10.value); 

      c=a/b;

      document.form1.result4.value=c;

    }

    num();

}

Interested in Java? Check out this Java Certification by Intellipaat.   

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 19, 2021 in Java by Harsh (1.5k points)
0 votes
1 answer
asked Feb 11, 2021 in Java by dante07 (13.1k points)

Browse Categories

...