This loops here calculate Bernoulli number which is further used to get the coefficient in the expansion of tan x
let B = 0 ; let Bn = 2 * i ; for ( let k = 0 ; k <= Bn ; k ++ ) { let temp = 0 ; for ( let r = 0 ; r <= k ; r ++ ) temp = temp + Math . pow ( - 1 , r ) * fac ( k ) * Math . pow ( r , Bn ) / ( fac ( r ) * fac ( k - r ) ) ; B = B + temp / ( k + 1 ) ; } sum = sum + Math . pow ( - 4 , i ) * ( 1 - Math . pow ( 4 , i ) ) * B * Math . pow ( x , 2 * i - 1 ) / fac ( 2 * i ) ; }