

 递归:在一个函数内部再一次调用自己;
效率:在本次调用还未结束时,就开始了下次的调用,本次调用就会被挂起,直到所有的调用都完成之后,才会依次返回。
形如数学函数:f(n)的阶乘
ex: 5!=5*4! (5*4*3*2*1)
4!=4*3! (4*3*2*1)
3!=3*2! (3*2*1)
2!=2*1! (2*1)
1!=1;
F(n)!=n*f(n-1)!
<script>
	//计算数字n的阶乘 (if方法)
	function f(n){
	if(n==1){ //边界条件
	return 1;
	}else{ //没到边界条件
	return n*f(n-1);
	}
	}
	/*或者:return n==1 ? 1 : n*f(n-1); (三目运算方法)
	function f(n){
	var result = return n==1 ? 1 : n*f(n-1);
	return result;
	}
	*/
	//计算5的阶乘?
	function testF(){
	var result = f(5);
	console.log(result);
	}
	testF();
	</script>	练习:
以下数列: 斐波那契数列
1,1,2,3,5,8,13,21,34,55……….
已知:该数列中的第一个数和第二个数都是1
从第三个数字开始,每个数字都是前两个数之和
问题: 用一个函数,求该数列中第n个数字的值
<!doctype html>
<html lang="en">
 <head>
	 <meta charset="UTF-8">
 <title>Document</title>
	 <link rel="stylesheet" style="text/css" href="">
	 <style>
	 </style>
 </head>
 <body>
	<script>
	 function f(n){
	if(n==1 || n==2){
	 return 1;
	 }else{
	 return f(n-1)+f(n-2);
	 }
	}
	function testF(){
	var result=f(20);
	console.log(result);
	}
	testF();
	</script>	
 </body>
</html>