calculate sum of all numbers
in an array : recursion
[ slice | length ]

Calculate sum of all numbers

Write a function that takes an array of numbers and returns the sum of all the numbers in that array.


Example ...

Enter an array ...

[1, 2, 3, 4, 5, 6] array

21 sum of all numbers in the array

1 + 2 + 3 + 4 + 5 + 6 = 21

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Calculate sum of all numbers using ↴

recursion → programming technique where a function calls itself repeatedly to solve a problem.

slice() method → returns selected elements in an array, as a new array.

length property → set or return the number of elements in an array.


Recursion The act of a function calling itself.

Recursion is used to solve problems that contain smaller sub-problems.

A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).

Use recursion to find the factorial of 5.

let x = 5;

function factorial(num) {

if (num > 1) { Recursion call

return num * factorial(num - 1);

}

else { Base case

return 1;

}

}

call function

factorial(x); returns ↴

120 factorial of 5 → 120


slice() method returns selected elements in an array, as a new array.

syntax ↴

slice(start) return a new array from start index to end of array

slice(start, end) return an array from start index to end index of array (exclusive).

Return a new array from index 1 to end of array.

const arr2 = [1, 2, 3, 4];

arr2.slice(1); start index is 1

returns ↴

[2, 3, 4]

Return a new array from index 1 to index 4 (exclusive).

const arr3 = [1, 2, 3, 4];

arr3.slice(1, 3); start index is 1 end index is 4 (not included)

returns ↴

[2, 3]


length property returns the number of elements in an array.

const arr4 = [1, 2, 3, 4, 5, 6];

arr4.length; returns ↴

6 → there are 6 elements in the array


Initialize a variable to hold the array to calculate the sum of all numbers.

const array1 = [2, 4, 6, 8, 10]; → user input


Define a function findSum to calculate the sum of all numbers in an array.

function findSum(arr) {}

The function takes an array as input arr and returns the sum of all numbers in that array.

Base case

If the array is empty, return 0 and end execution of function.

if (arr.length === 0) return 0

Recursive case

If the array is not empty, the function returns the first element of the array arr[0] added to the result of calling findSum on the rest of the array arr.slice(1)

The slice method creates a new array that excludes the first element.

return arr[0] + findSum(arr.slice(1))


Call the function with ↴

findSum(array1);


Calculate sum of all numbers in an array.

const array1 = [2, 4, 6, 8, 10];

function findSum(arr) {

if (arr.length === 0) return 0;

return arr[0] + findSum(arr.slice(1));

}

call function

findSum(array1); returns ↴

30

Calculate sum of all numbers in an array