JavaScript Calculate 2 Numbers - Addition Subtraction Multiplication Division

How To Calculate 2 Numbers Using Javascript

Javascript Calculate 2 Numbers



In This Javascript Tutorial we will See How To Calculate 2 Numbers ( Sum, Mutiplication, Division, Substraction ) Using Input Text, Button And Select Option In JS Using Netbeans Editor .




Project Source Code:

<!DOCTYPE html>

<html>
    <head>
        
        <title>javascript: calculate two numbers</title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <script>
            
            function calc()
            {
                var n1 = parseFloat(document.getElementById('n1').value);
                var n2 = parseFloat(document.getElementById('n2').value);
                
                var oper = document.getElementById('operators').value;
                
                if(oper === '+')
                {
                    document.getElementById('result').value = n1+n2;
                }
                
                if(oper === '-')
                {
                    document.getElementById('result').value = n1-n2;
                }
                
                if(oper === '/')
                {
                    document.getElementById('result').value = n1/n2;
                }
                
                if(oper === 'X')
                {
                    document.getElementById('result').value = n1*n2;
                }
            }
            
        </script>
        
    </head>
    <body>
        
        <input type="text" id="n1"/><br/><br/>
        <input type="text" id="n2"/><br/><br/>
        
        <select id="operators">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="/">/</option>
            <option value="X">X</option>
        </select>
        
        <button onclick="calc();">=</button>
        <input type="text" id="result"/>
        
    </body>
</html>


OUTPUT:

Javascript Calculate 2 Number




Share this

Related Posts

Previous
Next Post »

8 comments

comments
18 octobre 2017 à 20:19 delete

Can you help me with the html/javascript code of (n*90+200)*1.02 when user will give the value of n?

Reply
avatar
25 octobre 2017 à 20:46 delete

function calcTest(n) {
return((n*90+200)*1.02);
}
var n= 5;
document.write(calcTest(n));

Reply
avatar
31 octobre 2017 à 04:39 delete

Return num after multiplying it by ten.

Reply
avatar
16 avril 2019 à 15:52 delete

Please I was asked to write a program that accepts 2 values and performs both addition, multiplication, subtraction,division and highest value among the two values..I need help

Reply
avatar
Anonyme
29 janvier 2021 à 02:00 delete

Hvala ti stari reso si mi lajf pri vrboti

Reply
avatar
Anonyme
27 décembre 2022 à 23:54 delete

We are enter the strings it shows error to set the validation

Reply
avatar