Recursive function working

Nothing special it’s working, I’m as usual troubled with the instructions, or let’s say the lack of instructions.

I quote :
" Write a recursive function, sum(arr, n), that returns the sum of the first n elements of an array." Is this really an instruction ? from where to where this function should go over a million or not ? I’m sorry it’s way insufficient to me.


function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
return 0;
}else {
return sum(arr, n - 1 ) + arr[n - 1];
}
// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Safari/537.36.

Challenge: Replace Loops using Recursion

Link to the challenge:

The array arr contains elements. You need to make a recursive function that adds together (takes the sum) of the first n elements in the array arr.

Is there a specific part of the output you don’t understand? Do you know how to index into an array? Do you know how to access the first n elements? Do you know how to sum? What can I clarify?

The recursive part is hard, but recursion doesn’t have anything to do with the basic task that needs to be accomplished - only how the task is accomplished.

By the way, I added spoiler tags around your code. Whenever you post a full working solution and have questions about it, please use [spoiler] and [/spoiler] tags around your code.

1 Like

No the letter “n” is specific enough for me, that’s not the matter since I could write the code.

The instructions are most of the time incomplete to me. If I’m instructed to do a recursive function starting at some point and finishing somewhere else, I’m comfortable with that.

For instance, in the next challenge we have a list of names, and out of the blue comes a “Bob” who happens to love potatoes. But this Bob is nowhere in the code, how can that be ?

And another point, is the fact that i’m unable to check my code line by line if I want/need to, my only option is “Submit” button, but it does not explain what’s going on.

That is all that it’s about.

I forgot some important point here, I hate to copy without understanding what I’m doing. And I find myself doing like that too many times in this tutorial.

You can always asking clarifying questions on the forum if you are confused.

You can use console log statements or other debugging techniques to investigate your function’s behaviour before you click ‘Submit’.

I just wish that this kind of techniques would have been taught in a first stage, instead of checking them on internet.

Thank you for your time anyway

Which kind of techniques? Debugging?

I’d like to see them earlier in the curriculum, though that’s a bit of a chicken and the egg problem because they don’t make as much sense without context.

Though, no single order works for everyone, so there will always a place for good old questions and answers on the forum.

Got you.

I have started coding with Python, where you learn quite quickly how to check each function, variable and so, or their type. I felt very comfortable probably because I had some control on it and knew what was not working .

It’s not the same here and that got me confused I guess.

Anyway thanks again

is it the case that asks “No such contact”? it’snot there because your function will return that is not there, it is being tested that requirement

use the Ask for help button if you need help with that one too

I’m afraid you don’t get my point.

If there was a user input somewhere (which I would create), or if I could try some non existing names, I would be understanding that easier. That’s not the case here.

open a topic for that challenge if the issue is there

functions are reusable, in this case you are said that they take an array and a number, what those rapresent, how they relate to the outpur, and then you should make it work with any possible value of arr and n
the instructions are generic, because the function you are creating is also generic

you can always add a function call to try how it works

yeah, I’m not getting your point at all. Can you explain? maybe we can find a solution

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