javascript部份函数的实现

来源:互联网 发布:巨人网络有什么游戏 编辑:程序博客网 时间:2024/05/18 02:31

一、实现斐波纳契数列

function fibonacci(size) {  var first = 0, second = 1, next, count = 2, result = [first, second];  if(size < 2)    return "the request was made but it was not good"  while(count++ < size) {    next = first + second;    first = second;    second = next;    result.push(next);  }  return result;}
二、实现factorial(n)阶乘

function factorial(n) {
  return n < 2 ? 1: factorial(n-1)*n;
}
来源:http://www.csdn.net/article/2013-07-11/2816190-If-Hemingway-wrote-JavaScript

原创粉丝点击