# Basic JavaScript - Replace Loops using Recursion

hello good day. i can’t seem to get past this challenge. pls what should i do here?

the hint couldn’t help either. thanks.

Your code so far

``````function sum(arr, n) {
// Only change code below this line
if (n == 1 ) {
return n + 1;
} else {
return arr + sum(arr, n + 1);
}
// Only change code above this line
}
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36`

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

What do you intend this to mean?

i was thinking i needed to add + 1 to calc for the next arr value cause it says sum but looking at it now…i really do not know?

I think you should be focusing more on what do you expect the computer to do
when you say

arr + something

for eg. what would

arr + 2 give us?

let arr = 1 will give us 3.

Hold up for a second. Is arr a number?

i think so? is it not representing the unknown n value? or does it just stand for array?

sorry recursion is giving me a tough time…could you please explain in the simplest form that could help me understand.

Write a recursive function, `sum(arr, n)`, that returns the sum of the first `n` elements of an array `arr`.

What is `n`? What is `arr`?

It isn’t really possible to get the answer without understanding what the inputs are.

arr is an array of numbers and n is the numbers in the array.

provided that you know that an array of numbers is NOT the same thing as a single number…

what does arr + 1 give us?

array would have to be arr = [1 , 2]

But you cannot add a number and an array, so that syntax can’t work.

So, lets back up here.

What should `sum(arr, 5)` do?

sum the array data up till the fifth value.

That’d be pretty easy if you had the answer to `sum(arr, 4)` right?

yeah.
sum(arr, 5) should return sum of arr(n - 1) till the fifth value.

Did you see what I last posted?

I mean, finding the answer to `sum(arr, 5)` would be pretty easy if you already knew the answer to `sum(arr, 4)`, right?

ok i’m confused. can we start from scratch. like what is arr? i’m asking.

arr is a data structure called an array.

It is like a list of values.

It is denoted this way
`[1,2,3,4,5]`
or
`["an","array","of","strings"]`

Here’s the step that talks about arrays in the curriculum

If you go and look at that one and the following 3 or 4 steps, you can refresh your memory about arrays there.

`arr` is still an array of numbers

You said that `sum(arr, 5)` should

Wouldn’t finding the sum of the first 5 elements be really easy if you already knew the sum of the first 4 elements?

yeah.
so you’re trying to say that sum(arr, 4) = the sum of the array data till the fourth value?