I am new to javascript and I am trying to figure out how to dynamically pass on switch
as a function?
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)
CodePudding user response:
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)}`
console.log(string)
function chooseFunction (select) {
// 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);
}
}
CodePudding user response:
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);
CodePudding user response:
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)