Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:

I do not understand , what does that returns the sum of the first n elements of an array arr. means, especially sum of first n?

Your code so far

function sum(arr, n) {
  // Only change code below this line
  if(n == n){
    console.log(n)
    return n
  }
  // Only change code above this line
}

Your browser information:

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

Challenge Information:

Basic JavaScript - Replace Loops using Recursion

N is a number. You need to add together the first n elements.

How? e.g. one test says sum([2, 3, 4, 5], 3) should equal 9. so given ur example of adding together the first n element, it will be 3+3 which is 9

No, it’s saying to add 2, 3, and 4 together

Still not getting it, it says

that returns the sum of the first n elements of an array arr .

the n= 3, but it should be equal to 9, how? sum([2, 3, 4, 5], 3) should equal 9`. n is a singular, but it says elements of an array

What are the first three valuing that array? What’s arr[0], arr[1], and arr[2]?

it is 2,3,4 but when u say first three value, question doesn’t say first three, it says first n elements, does not say how many so how did u get number 3

It says right here in this sample function call that n is 3

then sum([2, 3, 4], 1) should equal 2. should be 5 becuase when it says 2, I am assuming 2+3 = 5 :thinking:

No. There n is 1. What’s the first 1 elements of that array?

1 Like

oh yea, got it. thank u so much

2 Likes

It’s good to make sure you know whatis being asked for before you start! It’ll definitely help you get to the answer

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.