Akuu426
November 7, 2022, 1:44pm
#1
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
}
```

Your browser information:

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:

Akuu426:

`arr + sum(arr, n + 1);`

What do you intend this to mean?

Akuu426
November 7, 2022, 10:13pm
#3
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?

hbar1st
November 7, 2022, 10:15pm
#4
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?

Akuu426
November 7, 2022, 10:20pm
#5
let arr = 1 will give us 3.

Hold up for a second. Is arr a number?

Akuu426
November 7, 2022, 10:25pm
#7
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.

Akuu426
November 7, 2022, 10:33pm
#9
arr is an array of numbers and n is the numbers in the array.

hbar1st
November 7, 2022, 10:35pm
#10
provided that you know that an array of numbers is NOT the same thing as a single number…

what does arr + 1 give us?

Akuu426
November 7, 2022, 10:41pm
#11
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?

Akuu426
November 7, 2022, 10:56pm
#13
sum the array data up till the fifth value.

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

right?

Akuu426
November 7, 2022, 11:02pm
#15
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?

Akuu426
November 7, 2022, 11:08pm
#17
ok i’m confused. can we start from scratch. like what is arr? i’m asking.

hbar1st
November 7, 2022, 11:13pm
#18
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?

Akuu426
November 7, 2022, 11:20pm
#20
yeah.
so you’re trying to say that sum(arr, 4) = the sum of the array data till the fourth value?