Tell us what’s happening:
Describe your issue in detail here.
Unfortunately, my issue is with the lesson itself, and how it’s very unclear and inadequate, especially for a concept that is a bit mind-boggling to come across for the first time (A function that calls itself???).
First line:
“Recursion is the concept that a function can be expressed in terms of itself.”
This very first line is already a bit iffy. I’ve seen this described more simply and clearly in a few places:
“In its simplest form, a recursive function is one that calls itself.” As explained by Beau Carnes
" Recursion is when a function calls itself until someone stops it. If no one stops it then it’ll recurse (call itself) forever." As explained by Yazeed Bzadough
"In the most basic of terms, recursion is when a function keeps calling itself until it doesn’t have to anymore. " As explained by Joel P. Mugalu
" A recursive function is a function that calls itself until it doesn’t. And this technique is called recursion." From javascripttutorial.net
IMO, these articles are pretty good at providing a much clearer, more detailed explanation of what recursion and recursive functions are, and maybe even critical to actually understanding the concept.
Going back to it, this following bit is from the lesson itself…
Using a
for
loop, you could do this:function multiply(arr, n) { let product = 1; for (let i = 0; i < n; i++) { product *= arr[i]; } return product; }
Ok, I understand this, this builds upon what we’ve learned from previous lessons.
And product must == 1 because it needs to start off the 1st item/number in the array (arr[0]).
So that whether it’s 1 or 5 or 10 or 57, the first number multiplied by 1 is itself,
before we move on to *= and the next number in the array.
But then going from the example using the for loop, the next line makes no sense whatsoever to a beginner unfamiliar with recursion:
" However, notice that
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
"
How in the world would I ever notice this?? It seems completely arbitrary. How does it make any sense to suddenly go from the for loop example to this line with no break down of why ‘multiply(arr, n)’ is equivalent to ‘multiply(arr, n - 1) * arr[n - 1]’ and how we came to this conclusion?
(I hate this. it’s like when they tell you in school to just memorize formulas without ever proving or explaining why they work. “Just accept what I tell you, you don’t need to know why.” Pretty sure that’s not why anyone gets into programming.)
If I substitute an actual array and number for n, for e.g.:
arr == [1, 2, 3, 4, 5] and n == 3, it begins to make some sense.
Step by step, this translates to:
Step 1: multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
Step 2: multiply([1, 2, 3, 4, 5], 3) == multiply([1,2,3,4,5], 3-1) * arr[3-1]
Step 3: multiply([1, 2, 3, 4, 5], 3) == multiply([1,2,3,4,5], 2) * arr[2]
Step 4: translated to human: multiply first 3 numbers of array [1, 2, 3, 4, 5] == multiply first 2 numbers of array [1, 2, 3, 4, 5] * 3rd item of array
Step 5: Ok yes, now I can see they are equivalent.
(Still no idea how this was deduced in the first place though. I’d like to know how the first person who came up with this did so. Mind-boggled)
Next, here’s the given code example following that very confusing, unclear declaration:
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
For the base case, why do you return 1 if n <= 0?
Why not return 0 since if the given n is 0 or less than 0, that would mean the instruction would be to multiply the first 0 (or less than 0) numbers in the array?
And isn’t the basic math that anything multiplied by 0 equals to 0?
This made it way more confusing for me.
IMO, I think this is the reason recursion (and anything else with a similar issue) is confusing because it’s a bit of mind-boggling concept, poorly explained, using jargon, sometimes with errors, and expecting those who are new to the concept to make leaps in understanding that are taken for granted by those who are already familiar.
And understanding and covering that gap makes the difference between great teachers and experts on subjects that are poor teachers. I’m sure we all know those teachers.
Another apt analog is poor UX vs. great UX. One makes software horribly confusing and frustrating to use, the other understands and meets the user where they are, and takes them through logically and intuitively.
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0
Challenge: Replace Loops using Recursion
Link to the challenge: