Sum of Numbers in an Array
NaN. This reinforces safe type checking, accumulator patterns, and the idea that you shouldn’t coerce strings like '5' into numbers unless the problem explicitly asks for it.Arguments
arr: any[] —An array containing numbers and possibly other types (e.g., strings, booleans, nulls). Only actual numbers should be summed.
Returns
number — 0 if there are no numeric entries.Solution idea
Iterate through the array, accumulate only valid numbers, and skip everything else. The key is to use typeof === 'number' and Number.isNaN so that NaN doesn’t poison the sum and strings/booleans aren’t implicitly coerced.
// Basic examples
sum([1, 2, 3]) // => 6
// '20' is a string → ignored
sum([10, '20', 30]) // => 40
// Ignores non-numbers
sum(['a', 5, true, 7]) // => 12
// Handles empty array
sum([]) // => 0Solution
Overview
Iterate through the array, accumulate only valid numbers, and skip everything else. The key is to use typeof === 'number' and Number.isNaN so that NaN doesn’t poison the sum and strings/booleans aren’t implicitly coerced.
Approach 1: Imperative loop (explicit and safe)
Steps:
1) Initialize total = 0.
2) For each value v in the array, check typeof v === 'number' and !Number.isNaN(v).
3) If valid, add it to the total.
4) Return the total after the loop.
Complexity: O(n) time, O(1) space. This is predictable and avoids hidden coercions.
export default function sum(arr) {
let total = 0;
for (const el of arr) {
if (typeof el === 'number' && !Number.isNaN(el)) {
total += el;
}
}
return total;
}
export default function sum(arr: any[]): number {
let total = 0;
for (const el of arr) {
if (typeof el === 'number' && !Number.isNaN(el)) {
total += el;
}
}
return total;
}
Approach 2: Filter + Reduce (functional style)
Idea: first filter the array to only include valid numbers, then sum them using reduce().
How it works:
- Use
filter()to keep only elements wheretypeof el === 'number' && !Number.isNaN(el). - Use
reduce()with an accumulator starting from0to sum the filtered values.
Why use it: concise and expressive, and the TypeScript version benefits from a type guard.
export default function sum(arr) {
return arr
.filter(el => typeof el === 'number' && !Number.isNaN(el))
.reduce((total, el) => total + el, 0);
}
export default function sum(arr: any[]): number {
return arr
.filter((el): el is number => typeof el === 'number' && !Number.isNaN(el))
.reduce((total, el) => total + el, 0);
}
Approach 3: Reduce-only with inline check
Idea: combine filtering and summing into a single reduce() call.
How it works:
- Start with
total = 0. - For each
elinarr, check if it is a valid number. - If so, add it to
total. - Return the final
total.
Why use it: avoids creating an intermediate array, which can be more efficient for large inputs.
export default function sum(arr) {
return arr.reduce((total, el) => {
if (typeof el === 'number' && !Number.isNaN(el)) {
total += el;
}
return total;
}, 0);
}
export default function sum(arr: any[]): number {
return arr.reduce((total, el) => {
if (typeof el === 'number' && !Number.isNaN(el)) {
total += el;
}
return total;
}, 0);
}
Notes & Pitfalls
Avoid coercing numeric strings — '20'should not be counted unless you explicitly convert it.Skip NaNto prevent polluting the final sum.Ensure non-numeric falsy values like false,null, orundefinedare not counted.
Empty array → returns 0.All non-numeric → returns 0.Includes NaN→ ignored.Mixed types → only valid numbers contribute to the total.
Type checking with typeofandNumber.isNaN.Using loops vs. functional array methods ( filter,reduce).Accumulator logic for numeric aggregation.
Common mistakes on this challenge
Sum should include numbers only
Non-number values and numeric strings should be excluded from the total. Filter with typeof x === "number"andNumber.isFinite(x)before summing.Do not coerce strings like "20"into numbers for this task.Use 0as the initial reducer value to handle empty arrays.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.