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.
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.
Let’s see if I can elaborate on the instructions. It seems the letter n might be throwing you off. If an array as 8 elements (see such an array below).
[ 2, 5, 3, 4, 10, 20, 30, 100]
If I want to look at the first 4 elements, then that means n = 4. Your function should return the sum of the first 4 elements (that would be the sum of 2, 5, 3, and 4 (or 14). Your function should be able to handle various arrays and values of n, which is why the instruction is worded generically as it is.
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.
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.
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.
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