This my code:
const addition = (num1, num2) => num1 + num2;
const subtraction = (num1, num2) => num1 - num2;
const multiplication = (num1, num2) => num1 * num2;
const division = (num1, num2) => num1 / num2;
const command = {
add: addition,
subtract: subtraction,
multiply: multiplication,
division,
execute: function(action, { num1, num2 }) {
return this[action](num1, num2);
}
};
const calculate = (action, numbers) => {
const result = command.execute(action, numbers);
console.log(
`In ${action} of numbers ${Object.values(numbers)} the result is ${result}`
);
};
calculate('add', { num1: 2, num2: 3 });
It is the command pattern that I did but I think if it could be the command like this or something that you declare operator to execute after to depend of action:
const command = {
add: '+',
subtract: '-',
multiply: '*',
division: '/',
execute: function(action, { num1, num2 }) {
return num1 this[action] numb2;
}
};
My question if this would be possible or the first code is good?
Because I see all the function in command is very similar the only change of the functions is the operator