Reputation: 129
function checkSalary(baseSalary, bonus, tax) {
console.log(baseSalary, bonus, tax)
function salary(baseSalary, bonus) {
console.log(baseSalary, bonus)
return baseSalary + bonus;
}
function salary_with_tax(tax) {
console.log(tax)
return salary() - tax;
}
}
console.log(checkSalary(5000, 3000, 100))
I want to check using salary() and salary_with_tax() but don't know how to pass the arguments.
Upvotes: 0
Views: 52
Reputation: 136104
Javascript is much like many languages where you can defined functions within other functions, but until you actually call the function nothing happens. Just call the inner functions and get the result and do with it whatever you like.
There is also no need to re-define the arguments if theyre exactly the same names.
function checkSalary(baseSalary, bonus, tax) {
function salary() {
return baseSalary + bonus;
}
function salary_with_tax() {
return salary() - tax;
}
const theSalary = salary()
const theSalaryWithTax = salary_with_tax();
console.log(theSalary, theSalaryWithTax);
return "Foo"; // you can "check" whatever you like here, and return something more appropriate
}
console.log(checkSalary(5000, 3000, 100))
Upvotes: 1
Reputation: 6512
You must call your inner functions at some point. Something like this:
I had to make some assumptions about what you want. I returned an object with the results of each inner function.
function checkSalary(baseSalary, bonus, tax) {
console.log(baseSalary, bonus, tax)
function salary(baseSalary, bonus) {
console.log(baseSalary, bonus)
return baseSalary + bonus;
}
function salary_with_tax(tax) {
console.log(tax)
return salary(baseSalary, bonus) - tax;
}
return {
salary: salary(baseSalary, bonus),
tax: salary_with_tax(tax)
}
}
console.log(checkSalary(5000, 3000, 100))
Upvotes: 1