Facebook Pixel

Sum of all numbers in an array

JavaScript
easy
15 mins

Given an array arr of valid finite numbers, return the sum of all elements.

Examples

Input: arr = [1, 2, 3] Output: 6
Input: arr = [5] Output: 5
Input: arr = [] Output: 0
Input: arr = [-1, 5, -4] Output: 0

Constraints

  • Input is always a valid array.
  • All elements are guaranteed to be finite numbers.
  • No need to validate for invalid inputs.

Function Signature

function sumArray(arr) { // Your code here }

Test Cases

  • Empty: []
  • Single element: [5], [0]
  • Small: [1, 2], [1, 2, 3]
  • Mixed values: [-1, 2, -3, 4], [10, -5, 5, -10]
  • Large sets: [1..10], [10, 20, 30, 40, 50]
  • With decimals: [1.5, 2.5, 3], [-1.2, 2.2]

Companies:

amazon
Accenture
infosys
capgemini
tcs
wipro

Solve Similar questions 🔥

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.