1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| function factorial(n) { return n == 1 ? n : n * factorial(n - 1); } console.log('阶乘', factorial(4));
var fibonacci = function(n) { return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); } console.log('斐波那契', fibonacci(10));
var reverseArrary = function(x, index, str) { return index == 0 ? str : reverseArrary(x, --index, (str += " " + x[index])); } var arr = ['apple', 'orange', 'peach', 'lime']; var str = reverseArrary(arr, arr.length, ""); console.log('str', str);
var orderArray = function(x, i, str) { return i == x.length - 1 ? str : orderArray(x, ++i, (str += x[i] + " ")); }
var numArr = [1, 2, 3, 4]; var numStr = orderArray(numArr, -1, ""); console.log('numStr', numStr);
|