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




9 commentaires:

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

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

      Supprimer
  2. Return num after multiplying it by ten.

    RépondreSupprimer
  3. 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

    RépondreSupprimer
  4. Hvala ti stari reso si mi lajf pri vrboti

    RépondreSupprimer
  5. We are enter the strings it shows error to set the validation

    RépondreSupprimer
  6. how to change dropdown into buttons

    RépondreSupprimer