const sum = arr => arr.flat().reduce((sum, x) => sum += (x = parseFloat(x)) ? x : 0, 0) alert(sum([1, 2, '3x'])); alert(sum([1, 2, 'x3'])); alert(sum([1, [1, 2], 2]));