Problem in division and multiplication

Asked

Viewed 161 times

0

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. However certain sum and subtraction.

<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>    
        <script type="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>

3 answers

1


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

You 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 several declared variables and sequence, provided you separate each one by comma. It’s a syntax I like. Keeps everything simpler as long as it’s well indented.

  • I can’t believe I was racking my brain over this :/. Thank you!

  • If it helped you, give a upvote. If you’ve solved your problem, mark your answer.

  • 1

    I didn’t know about this rule of syntax, it helps well, and it makes the code cleaner. It just makes it right. Vlw

  • Good luck! If you can study Jon Ducket’s book on Javascript. It’s worth it!

1

change:

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

for:

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

'#' is selector for element via id attribute

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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.