Home > other >  How to write javascript code for income tax calculator according to gender?
How to write javascript code for income tax calculator according to gender?

Time:10-11

If salary is 50000 then it will print tax is not applicable. If amount between 50001 to 150000 then for male it will be 10% and for female it'll be 7% If greater than 150000 it will be 15% for men and 10% for female

I'm stuck in this....

function calculateTax(){ let sal = document.getElementById("salary").value; let m = document.getElementById("male").Checked; let f = document.getElementById("female").Checked; if (sal

CodePudding user response:

The easiest way to complete this task is if construction. For example:


    const salary = 50000;
    const gender = "M";
    
    let tax = 0;
    
    if(salary <= 50000) {
     console.log("tax is not applicable")
    } else if(salary > 50000 && salary <= 150000) {
     if(gender == "M") {
      tax = 10;
     } else {
      tax = 7;
     }
    } else {
     if(gender == "M") {
      tax = 15;
     } else {
      tax = 10;
     }
    }

You can also do it in many different ways. In class where implement gender and salary to constructor for exmaple:

class TaxCalculator {
 ...code inside.
}

Hope it help at start.

CodePudding user response:

Use statement & Ternary Operator

  function incomeTax(salary, gender) {
    if (salary <= 50000) {
      return "Tax is not applicable";
    } else if (salary > 50000 && salary <= 150000) {
      return gender === "Male" ? salary * 0.1 : salary * 0.07;
    } else if (salary > 150000) {
      return gender === "Male" ? salary * 0.15 : salary * 0.1;
    };
  };

  console.log(incomeTax(160000, "Male")); // 24000
  • Related