Answer the question
In order to leave comments, you need to log in
An even simpler and more practical example of recursion?
Recursion example:
implement the formula xn= x*xn-1
function power(x,n){
if(n == 1) {
return x;
}
else {
return x*power(x,n-1);
}
}
console.log(power(2,3));
Answer the question
In order to leave comments, you need to log in
Let me give you an example that I used the other day. Unfortunately, not in JS, but in PHP, and it is not so simple, but maybe it will help you a little:
public function post()
{
if (!$this->parent()) {
return $this->commentable_id;
}
else {
return $this->parent()->post();
}
}
Recursion is great if you need to render the DOM from a tree dataset.
I once made a widget that is embedded on sites. In order not to make an iframe (as it was necessary) and not to write tons of the same createElement and appendChild, you simply feed the config function in JSON, and it parses it and renders the elements.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question