Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

328
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda