September 3rd, 2024
What's missing in this code?
// addNumbers(5, '15'); // Output: 20
// addNumbers([1, 2, 3], '10'); // Output: 16
// addNumbers('5', [1, 2, 3, 4]); // Output: 15
// addNumbers([1, 2, 3], [4, 5]); // Output: 15
function addNumbers(a, b) {
const convertToNumber = (input) => {
if (
===
) {
return input;
} else if (typeof input === 'string') {
return
(input);
} else if (
(input)) {
return input.reduce(
(acc, current) =>
,
0
);
} else {
throw new Error('Invalid input type');
}
};
const numA = convertToNumber(a);
const numB = convertToNumber(b);
return numA + numB;
}
Type or select from these options