Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:
The Question: Write a recursive function, sum(arr, n), that returns the sum of the first n elements of an array arr.
How are we meant to discern what n represents from this question? I cannot see any indication of what n is meant to represent, index? Is this question just poorly written, or dose, first n elements, have a specific meaning? I don’t want to be just told what it means, I want to know how to discern what it means from this question.
n represents number of elements.

Your code so far

function sum(arr, n) {
  // Only change code below this line
  if (n <= o) {
    return 0;
  }
  else {
    return num(n + arr[])
  }
  // 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/107.0.0.0 Safari/537.36 Edg/107.0.1418.62

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

Yes, it means the first n elements in the array passed into the function. So if you called this function as:

sum([1,2,3,4,5,6,7,8,9,10], 5);

Then n would be 5 and that would mean to only sum the first five elements in the array, which would be 1 + 2 + 3 + 4 + 5.

I get it, the first n elements, once an argument is passed is, the first 5 elements. (Using your example). Taking more notice of the arguments from now on will help me decode what the parameters stand for.
Thank you.

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