javascript部份函数的实现

一、实现斐波纳契数列

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

你可能感兴趣的:(js)