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

323
Visualizações
Change the argument of a function that is inside another function

This are my tasks:

This is my tasks

As you can see, they all have "same" logic. So I wanted use function like this ->

const task71=(x)=>Math.tan(2*x+x*x);
const main=(x,dX,xEnd)=>{
  while(x<=xEnd)
  {
    task71(x); //this part I wanna change, so it will work for task72,task73...
    x+=dX;
    console.log(`Y=${task71(x)}\nX=${x}`);
  }
}

This works only for task71(x),but I have task72(x),task73(x),etc. What do I need to change in main(x,dx,xEnd) function to make it work for another functions? I tried use task71(x) as argument, but in this way the function only works with the initial value of x.

Perhaps this question is stupid, sorry, I recently started learning programming.

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

0

You can load all your taks into an object, and then execute each task in your main:

const tasks = {
  71: (x)=>Math.tan(2*x+x*x),
  73: (x)=>Math.pow(x+1, 2),
}
const main=(x,dX,xEnd)=>{
  while(x<=xEnd)
  {
    for(let id in tasks)
    {
      let y = tasks[id](x); //this part I wanna change, so it will work for task72,task73...
      console.log(`task${id}:\nY=${y}\nX=${x}`);
    }
    x+=dX;
  }
}

main(2.40,0.20,7.60);

Alternatively you can pass the task function to the main:

const tasks = {
  71: (x)=>Math.tan(2*x+x*x),
  73: (x)=>Math.pow(x+1, 2),
}

const main=(x,dX,xEnd, taskId)=>{
  while(x<=xEnd)
  {
    let y = tasks[taskId](x); //this part I wanna change, so it will work for task72,task73...
    x+=dX;
    console.log(`task${taskId}:\nY=${y}\nX=${x}`);
  }
}


for(let id in tasks)
{
  main(2.40,0.20,7.60, id);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you want something like this:

const taskmap = {
  task71: (x)=>Math.tan(2*x+x*x);
  // etcetera
};

And then:

const main=(x,dX,xEnd)=>{
  func_count = 71;
  while(x<=xEnd)
  {
    var fn = taskmap['task'+func_count];
    fn(x);
    x+=dX;
    console.log(`Y=${fn(x)}\nX=${x}`);
  }
}
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