Achieve Fibonacci columns of es5, es6

es5 achieve Feibolaqi logarithmic function columns:

<Script type = "text / JavaScript">
         function Fibonacci (n-) {
             var One =. 1 ;    
             var TWO =. 1 ;    
             for ( var I =. 3; I <= n-; I ++) {    // Here key part of code, with before three cumulative two numbers and also the essence of Fibonacci numbers Fibonacci sequence lies. 
                var Three One = + TWO;      
                one = two;      
                two = three;      

            }
            IF (n == 1 == || n-2) {    // Analyzing return undefined case where n == 1 or 2, 
                return One;
            }
            return Three;       // finally returns Three 
        }
        console.log(fibonacci(2));
    </script>

es6 achieve Feibolaqi logarithmic function columns:

function* fibonacci() {
  let [prev, curr] = [0, 1];
  for (;;) {
    yield curr;
    [prev, curr] = [curr, prev + curr];
  }
}

for (let n of fibonacci()) {
  if (n > 1000) break;
  console.log(n);
}

Reference link: https: //www.cnblogs.com/bitkuang/p/9711304.html

     http://es6.ruanyifeng.com/#docs/generator

Guess you like

Origin www.cnblogs.com/pengxiangchong/p/11639952.html