<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>御用递归,下回分解</title>
<style type="text/css">
</style>
<script type="text/javascript">
var array=[
{"id":1,"child":[{"name":"11"},{"name":"12"},{"name":"13"}]},
{"id":1,"child":[{"name":"21"},{"name":"22"},{"name":"23"}]},
{"id":1,"child":[{"name":"31"},{"name":"32"},{"name":"33"}]},
]
for ( var i = 0 i < array[0].child.length i++)
{
for ( var j = 0 j < array[1].child.length j++)
{
for ( var k = 0 k < array[2].child.lengthk++)
{
document.write(array[0].child[i].name + "," + array[1].child[j].name + "," + array[2].child[k].name + "<br / >")
}
}
}
</script>
</head>
<body>
</body>
</html>
var a1,a2,a3,a4,a5,a6,a7,a8,a9,a10// 递归赋值
setval(1, 100)
// 打印验证
document.write('打印验证:<br>')
for (var i=1i<=10i++) {
document.write('a'+i+' = ')
eval('document.write(a'+i+')')
document.write('<br>')
}
function setval($var_i, $var_val) {
if ($var_i > 10) return false
eval('a' + $var_i + ' = ' + $var_val + '')
$var_i ++
$var_val += 100
setval($var_i, $var_val)
}
<!DOCTYPE >
<html>
<head>
<meta content="" charset="utf-8">
<title>函数的递归调用</title>
</head>
<body>
<script>
//递归的概念:自己调用自己
//注意:使用递归的时候必须有一个结束标志,否则会报内存溢出的错误 Maximum call stack size exceeded
/* 1.案例一:求1,2,3...n 的和 */
function fn(n){
if(n===1){
return 1
}
return n+fn(n-1)
}
//console.log(fn(3))
/* 2.案例二:求1,2,3...到n的阶乘 */
function getFactorial(n){
if(n===1){
return 1
}
return n * getFactorial(n-1)
}
//console.log(getFactorial(3))
/* 案例三:斐波那契数列 *///第n个数等于前两个数的和,除第一个数跟第二个树外:如1,1,2,3,5,8,11,19,30...
function getNFibonacciSequence(n){
if(n===1 || n===2){
return 1
}
return getNFibonacciSequence(n-1)+getNFibonacciSequence(n-2)
}
console.log(getNFibonacciSequence(4))
</script>
</body>
</html>