+ 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...

13th Dec 2017, 2:59 PM
Michael King
Michael King - avatar
26 Answers
+ 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.
13th Dec 2017, 3:47 PM
AgentSmith
+ 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!
13th Dec 2017, 3:21 PM
Oma Falk
Oma Falk - avatar
+ 2
If you wish, run this. It shows the levels at exit. https://code.sololearn.com/WgCYZQ5l1y5y/?ref=app
13th Dec 2017, 3:40 PM
John Wells
John Wells - avatar
+ 1
Thanks Netkos Ent Alot!!! u didn't just answer the question u motivated me... Thanks once again.√
13th Dec 2017, 4:51 PM
Michael King
Michael King - avatar
0
k thanks batman
13th Dec 2017, 3:27 PM
Michael King
Michael King - avatar
0
@John Wells.. The result is always 27, no matter the base or exponient
13th Dec 2017, 4:54 PM
Michael King
Michael King - avatar
0
lemme try it again
13th Dec 2017, 5:00 PM
Michael King
Michael King - avatar
0
still the same
13th Dec 2017, 5:01 PM
Michael King
Michael King - avatar
0
k
13th Dec 2017, 5:05 PM
Michael King
Michael King - avatar
0
Still won't work
13th Dec 2017, 5:08 PM
Michael King
Michael King - avatar
0
Uncaught reference error: perform is not defined. line 27
13th Dec 2017, 5:11 PM
Michael King
Michael King - avatar
0
No
13th Dec 2017, 5:13 PM
Michael King
Michael King - avatar
0
k
13th Dec 2017, 5:17 PM
Michael King
Michael King - avatar
0
The same result. But it's working perfectly in ur phone?
13th Dec 2017, 5:18 PM
Michael King
Michael King - avatar
0
hmm
13th Dec 2017, 5:40 PM
Michael King
Michael King - avatar
0
how?
13th Dec 2017, 5:47 PM
Michael King
Michael King - avatar
- 1
I'm not sure what your getting. I get right answers in console log
13th Dec 2017, 4:59 PM
John Wells
John Wells - avatar
- 1
put 2&2,
13th Dec 2017, 5:02 PM
John Wells
John Wells - avatar
- 1
I get power(2,0)=1,power(2,1)=2,power(2,2)=4
13th Dec 2017, 5:04 PM
John Wells
John Wells - avatar
- 1
what output did you get?
13th Dec 2017, 5:09 PM
John Wells
John Wells - avatar