+ 2
Please Help!!!
I'm learning javascript for the first time and its really confusing which material will be the best to learning efficiently. But currently I'm using a material called Eloquent Javascript and there is a piece of code I don't quite get.... function power(base, exponient) { if(exponient == 0) return 1; else return base * power(base, exponient - 2); } Please I need more clarification. The code actually tries to explain recursion, but I don't quite get it...
26 ответов
+ 4
Basically, you're calling the same function from within the function, so it creates a loop of itself until certain conditions are met. In this case, it's going to keep looping until "exponient" is equal to 0. Otherwise it'll keep running and subtracting 2 from "exponient" until it does equal 0 and thus the recursive function ends.
As for learning from that book, there is no reason for you not to. Don't shy away from things just because it may or may not be challenging, otherwise you won't last long as a developer. Recursion and the example above isn't advanced programming, it's actually a beginner topic, so just learn it and play around with it until it makes sense for you. That's how you learn new things because new things always provide a certain level of challenge until you take the time to learn it. With that being said, use all resources that you have available and learn what you can from all directions.
+ 2
@Michael stop learning from that book. It is not for beginners.
Recursion is a general algorithm idea.
calculate 10**n by calculating 10**(n-1) * 10
if n is 0, the result is 1.
dont try to understand thr sense, just how it works!
+ 2
If you wish, run this. It shows the levels at exit.
https://code.sololearn.com/WgCYZQ5l1y5y/?ref=app
+ 1
Thanks Netkos Ent Alot!!! u didn't just answer the question u motivated me... Thanks once again.√
0
k thanks batman
0
@John Wells.. The result is always 27, no matter the base or exponient
0
lemme try it again
0
still the same
0
k
0
Still won't work
0
Uncaught reference error: perform is not defined.
line 27
0
No
0
k
0
The same result.
But it's working perfectly in ur phone?
0
hmm
0
how?
- 1
I'm not sure what your getting. I get right answers in console log
- 1
put 2&2,
- 1
I get power(2,0)=1,power(2,1)=2,power(2,2)=4
- 1
what output did you get?