Please translate from Math's to English

Please translate from Math's to English
0

Tell us what’s happening:
Could someone please explain to me, in the most simple and non-mathematical terms, what this sentence means; " Write a recursive function, sum(arr, n) , that returns the sum of the elements from 0 to n inclusive in an array arr . Hints: sum([1], 0) should equal 1, sum([2, 3, 4], 1) should equal 5.

I’m not looking for an explanation of recursion or how for-loops work. I’m looking for a simple explanation of the mathematical process going on in the highlighted section that returns the sum of the elements from 0 to n inclusive in an array arr . Pretty simple sentence really but I’m native English and this makes no sense to me, it has been many many years since I was taught basic maths.

Your code so far


function sum(arr, n) {
// Only change code below this line

// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36.

Challenge: Replace Loops using Recursion

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion

What it basically says is that you need to sum all elements in an array up to the index provided which is n. For example your array is [2, 3, 4] and n in that example is 1 so index 0 represents 2 in that array and index 1 which is n represents 3 so you need to add 2 and 3.

2 Likes

You get two arguments, array and index. You should return the sum of all values from array[0] to array[index].

So in the example given, sum([3, 2, 4], 1) would return 3 + 2 = 5, and sum([3, 2, 4], 2) would return 3 + 2 + 4 = 9.

1 Like

Sum means add up the given values: “the sum of one and one and one is three”. Inclusive means a given range of values includes the first and last. “I’m taking my holiday between March the first and March the tenth inclusive” doesn’t mean the holiday is March 2nd to March 9th, but if you missed off the “inclusive” that may be what your HR department interprets your request as.

You have a collection of numbers, add them all up. Ranges of numbers are normally specified as either inclusive or non-inclusive in most programming contexts, as the distinction is important (some languages have special syntax for ranges, and they often explicitly specify the difference – for example 0..10 vs. 0...10).

1 Like

That was incredibly simple, thanks all. One of those rage moments where you lose your basic faculties.

2 Likes