piffy

3.5.9 Es 4

Aug 29th, 2021
1,086
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function fibo(n) {
  2.   if (n<1) return undefined;
  3.   arr=fiboarray(n);
  4.   return arr[n];
  5. }
  6.  
  7. export function fibosomma(n) {
  8.   if (n<1) return undefined;
  9.   arr=fiboarray(n);
  10.   let somma=0;
  11.   for(let i in arr) { somma += arr[i]; }
  12.  
  13.   //Soluzione ES6: somma=arr.reduce((a, b) => a + b, 0);
  14.   return somma;
  15. }
  16.  
  17. export function fiboarray(n) {
  18.   if (n<1) return undefined;
  19.   let fib = [];
  20.   fib[0] = 0;
  21.   fib[1] = 1;
  22.   for (let i = 2; i <= n; i++) {
  23.     fib[i] = fib[i - 2] + fib[i - 1];
  24.   }
  25.   return fib;
  26. }
  27.  
  28.  
RAW Paste Data