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

162
Vistas
Passing on function with switch dynamically

I am new to javascript and I am trying to figure out how to dynamically pass on functions with switch?

For example, I can pass on function manually like following

var select =1 ;
function add (val){ return val+512};
function subtract(val) {return val-300};
function multiply(val){return val*584};
var value = 290;

var text = `the value is ${add(value)}`

console.log(text);

How can I pass on a function with a switch based on the select value? The following does not work.

var select =1 ;
function add (val){ return val+512};
function subtract(val) {return val-300};
function multiply(val){return val*584};
var value = 290;

//switch statement
switch(select){ case 1: add(value); break; case 2: subtract(value); break; case 3: multiply(value); break};

var string = `this value is ${switch(select){ case 1: add(value); break; case 2: subtract(value); break; case 3: multiply(value); break};}`

console.log(string)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I suggest moving the switch bloc inside a function, like the following:

var select = 1;
function add(val) { return val + 512 };
function subtract(val) { return val - 300 };
function multiply(val) { return val * 584 };
var value = 290;



var string = `this string in ${chooseFunction(select, value)}`

console.log(string)

function chooseFunction (select, value) {
  // switch statement

  // Note that the break statement is optional 
  // if you return a value at the end of a case block
  switch (select) {
    case 1: return add(value); 
    case 2: return subtract(value); 
    case 3: return multiply(value); 
  }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You write a function that has the switch returning the result of calling functions based on the input, and use that in the template string.

function add(val) {
  return val + 512;
}

function subtract(val) {
  return val - 300;
}

function multiply(val) {
  return val * 584;
}

function run(value, select) {
  switch (select) {
    case 1: return add(value);
    case 2: return subtract(value);
    case 3: return multiply(value);
  }
}

const value = 290;

const string1 = `This string is ${run(value, 1)}`;
const string2 = `This string is ${run(value, 2)}`;
const string3 = `This string is ${run(value, 3)}`;

console.log(string1);
console.log(string2);
console.log(string3);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You cannot pass a switch directly, as you cannot pass a if statement without a ternary operator. You need to encapsulate your switch statement in a function like this :

var select =1 ;
function add (val){ return val+512};
function subtract(val) {return val-300};
function multiply(val){return val*584};
var value = 290;

const getValue = (s, v) => {
switch(s) {
  case 1:
    return add(v);
  case 2:
    return subtract(v);
  case 3:
    return multiply(v);
   };
}

var string = `this value is ${getValue(select, value)}`

console.log(string)

An other way to do this is to put your functions into an array with the index referencing the choice :

var select =1 ;
function add (val){ return val+512};
function subtract(val) {return val-300};
function multiply(val){return val*584};
var value = 290;

const fn = [add, subtract, multiply];

var string = `this value is ${fn[select - 1](value)}`

console.log(string)

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