Type a collection
Difficulty: Easy
Try your first run
No account is needed to try this exercise. Write your answer, run the tests, and use the feedback to improve it.
- Read the examples and the required return value.
- Edit the supplied function in the editor, keeping its name and parameters. Return your answer, then select Run code.
- Use the test feedback to try again. Hints are available without replacing your code.
Return the sum of finite numbers, or zero for an empty list.
Examples
[]0[1, 2, 3]6[-2, 0, 2]0Constraints
Use strict TypeScript and a standalone function. No imports, packages, DOM, network, or file access. Inputs are JSON-compatible with at most 1,000 elements and 100,000 characters. Do not mutate inputs. Return the specified result rather than printing. Medium tasks combine narrowing and typed transformations.
Review lesson: Inference and explicit boundariesA little nudge
Reveal one hint at a time. Give yourself a moment to try it.
- Use readonly number[] to forbid mutation through this parameter.
- Supply the empty-input accumulator.
Worked solution
Try your own approach first, then compare the reasoning.
function sumValues(values: readonly number[]): number {
return values.reduce((total, value) => total + value, 0);
}
A readonly parameter communicates non-mutation without changing runtime storage.
Complexity
O(n) time and O(n) output space for n input elements; scalar operations use O(1).
Ready when you are
Write your solution, then run it against the practice tests.
Press Esc, then Tab to leave the editor. Code runs in an isolated browser workspace. Tests support learning, not verified grading.