1

I couldn't add 3 numbers from inputs together. instead of adding them, it displays the numbers. Ex: 1+2+3= it becomes 123 instead of 6. It works well for the Earning but the deduction is causing the problem. any suggestion?

This is my script:

<script>
    function getTotal() {
        //total earnings
        var morning_rate = document.salaries.morning_rate.value;
        var morning_day = document.salaries.morning_day.value;
        var night_rate = document.salaries.night_rate.value;
        var night_day = document.salaries.night_day.value;
        var earning = ((morning_rate * morning_day) + (night_rate * night_day));
        //DEDUCTION
        var uniform = document.salaries.uniform.value;
        var damage = document.salaries.damage.value;
        var othe = document.salaries.others.value;
        var totalDec = (uniform + damage + other);
        //NET SALARY
        var net_salary = (earning - totalDec);
        //RESULT
        document.getElementById('total_earning').value = earning
        document.getElementById('total_dec').value = totalDec
        document.getElementById('net_salary').value = net_salary
    }
    </script>
kedar sedai
  • 1,687
  • 3
  • 16
  • 25
Lisa
  • 13
  • 3
  • 2
    Try wrapping the values into parseFloat() function before the operation like this `var uniform=parseFloat(document.salaries.uniform.value);` – Nick Surmanidze Jun 02 '20 at 08:36
  • or just do `var uniform= +document.salaries.uniform.value;` Check this out: https://stackoverflow.com/questions/12120802/explain-var-and-var-unary-operator-in-javascript – Cornel Raiu Jun 02 '20 at 08:45
  • you don't need parenthesis by the way because the '*' (multiplication) will be done before '+' (addition) and '-' (subtraction) – SaboSuke Jun 02 '20 at 09:17

2 Answers2

0

Your written code is almost okay. I have modified so. I hope it will work fine..

 //total earnings
    var morning_rate=parseFloat(document.salaries.morning_rate.value);
    var morning_day=parseInt(document.salaries.morning_day.value);    
    var night_rate=parseFloat(document.salaries.night_rate.value);
    var night_day=parseInt(document.salaries.night_day.value;

    var earning=((morning_rate*morning_day) +(night_rate*night_day));

    //DEDUCTION

    var uniform=parseFloat(document.salaries.uniform.value);
    var damage=parseFloat(document.salaries.damage.value);
    var othe=parseFloat(document.salaries.others.value);

    var totalDec=(uniform+damage+other);

    //NET SALARY
    var net_salary=(earning-totalDec);

   //RESULT
    document.getElementById('total_earning').value=earning.toFixed(2);
    document.getElementById('total_dec').value=totalDec.toFixed(2);
    document.getElementById('net_salary').value=net_salary.toFixed(2);

Cheers...

0

The value of an input field, by default, is a string, and + concatenates a string to the end of the other string. To treat these strings as integers, you have to use parseInt() function, which takes a string, float, or a boolean as an argument and returns an integer. For example, you should use:

var damage = parseInt(document.salaries.damage.value);

Instead of

var damage=document.salaries.damage.value;
Talha Quddoos
  • 556
  • 6
  • 17