I need to overload a function overloadedFunc() which takes 3 arguments. For the 1st argument of the function set the default value [1, 2, 3], for the 2nd - the value 2, for the 3rd - the function that returns the product of the first two arguments, and the function can multiply both arrays and numbers. The overloadedFunc() function returns the result of the default function. I have a code but it throws an error. How can I solve this problem?
function overloadedFunc(arg1 = [1, 2, 3], arg2 = 2, arg3 = multiply()) { const res = multiply(arg1, arg2); return res; } function multiply(arg1, arg2) { arg1.forEach((item) => { item * arg2; }); } console.log(overloadedFunc()); // [2, 4, 6] console.log(overloadedFunc([2, 4, 6], 3)); // [6, 12, 18] console.log(overloadedFunc(10)); // 20
forEachproperty ofundefined. Look at the stack trace to see why it is undefined.arg3 = multiply(without the call)?arg3issue when you calloverloadedFunc(10), since10is a value (and thereforarg1doesn't use the default), but does not have a.forEach()