How to calculate the sum of series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N! in JavaScript

1 Answer

0 votes
function factorial(n) {
    if (n <= 1) {
        return (1);
    } else {
        n = n * factorial(n - 1);
    }
 
    return (n);
}


console.log("Calculate the sum of series: 1/1! + 2/2! + 3/3! + 4/4! ... + N/N!");

const N = 5; 
let sum = 0.0;
 
for (let i = 1; i <= N; i++) {
    sum  +=  i / factorial(i);
}

console.log("sum = " + sum);
 
 
 

         
/*
run:
 
"Calculate the sum of series: 1/1! + 2/2! + 3/3! + 4/4! ... + N/N!"
"sum = 2.708333333333333"
      
*/

 



answered Nov 5, 2016 by avibootz
edited Mar 28, 2022 by avibootz
...