Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
how to pass argument to child function in js
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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda