Problem in division and multiplication

1

I have an exercise to do the four basic operations, but multiplication / division is not going, and you have to test if the number q is dividing is not zero. But sum and subtraction is right.

<html>
    <head>
        <title> Primeiro Projeto</title>
        <meta charset="utf-8">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scripttype="text/javascript">
            function calcSoma(){
                var a=$("#num1").val(); 
                var b=$("#num2").val();
                var c= parseInt(a)+parseInt(b);
                $("#num3").val(c);
            }
            function calcSub(){
                var a=$("#num1").val(); 
                var b=$("#num2").val();
                var c= parseInt(a)-parseInt(b);

                $("#num3").val(c);
            }
            function calcDiv(){
                var a=$("num1").val();
                var b=$("num2").val();
                if(b != "0"){
                    var c= parseInt(a)/parseInt(b);
                    $("#num3").val(c);
                }else{
                    $("#num3").val("Dividendo é 0");                    
                }
            }
            function calcMult(){
                var a=$("num1").val();
                var b=$("num2").val();
                var c= parseFloat(a)*parseFloat(b);

                $("#num3").val(c);
            }

        </script>

    </head>
    <body>

        Número1: <input type="text" size="2px" id="num1"><br>
        Número2: <input type="text" size="2px" id="num2"><br>       
        <button onclick="calcSoma()">+</button>
        <button onclick="calcSub()">-</button>
        <button onclick="calcDiv()">/</button>
        <button onclick="calcMult()">*</button><br>
        Resultado:<input type="text" size="2px" id="num3">


    </body>

</html>
    
asked by anonymous 12.08.2017 / 05:00

3 answers

1

Simple, you forgot the octothorpe or cert in id in both division and multiplication:

You have put:

var a=$("num1").val();

Should be:

var a=$("#num1").val();

TIP

function calcMult() {
    var a = $("num1").val(),
        b = $("num2").val(),
        c = parseFloat(a) * parseFloat(b);

    $("#num3").val(c);
}

You can use var only once for multiple declared variables and sequence, as long as you separate each by a comma. It's a syntax I like. It keeps everything simpler as long as it is well indented.

    
12.08.2017 / 05:09
1

Switch:

var a=$("num1").val();
var b=$("num2").val();

To:

var a=$("#num1").val();
var b=$("#num2").val();

'#' is selector for element through attribute id

    
12.08.2017 / 05:12
0

In one of my needs similar to yours, I came to this conclusion:

var sum = $('.larg').text(); 
var sum2 = $('.alt').text();
var sum3 = $('.metro').text();

if(sum == "") sum = 0;
if(sum2 == "") sum2 = 0;
if(sum3 == "") sum3 = 0;

var resultado = parseInt(sum) * parseInt(sum2) * parseInt(sum3);
    
12.08.2017 / 05:22