const sum = function sum(arr) {
arr = arr.map(n => Array.isArray(n) ? sum(n) : parseFloat(n) || 0);
return eval(arr.join('+'));
}
alert(sum([1, 2, '3x']));
alert(sum([1, 2, 'x3']));
alert(sum([1, [1, 2], 2]));
or
const sum = arr => eval(arr.map(n => Array.isArray(n) ? sum(n) : parseFloat(n) || 0).join('+'));
alert(sum([1, 2, '3x']));
alert(sum([1, 2, 'x3']));
alert(sum([1, [1, 2], 2]));
or
const sum = function sum(arr) {
let res = 0;
for (const n of arr) {
res += Array.isArray(n) ? sum(n) : parseFloat(n) || 0;
}
return res;
}
alert(sum([1, 2, '3x']));
alert(sum([1, 2, 'x3']));
alert(sum([1, [1, 2], 2]));