# Some math functions involving loops

#1

I defined a function that sums up all elements of an array, it works as long as you input a number.

To test it i also defined a function that returns the factorial of the input if its a number and with that I created a loop that pushes the first nth terms of an infinite series who's limit is the Euler number into an array.

Hope it helps someone, if you find any mistake or know a better way to do the same tell me.

``````// factorial function//

function factorial(x){
var fact = 1
for(j=1; j <= x; j++){
var fact = fact*j;
}
return fact;
}

//This function sums all the elements of a given array, if the array represents the partial sum up to n terms
// of an infinite series, this would yield the limit of the given series//
function limit(params) {
var summation = 0;
var result = 0;
for (i=0; i<params.length; i++) {
summation += params[i];
}
var tempresult = summation.toFixed(15);
var intresult = Math.round(tempresult);
// if the difference between the "float" result and the "int" result is negligible we will return the integer value,
//else we will return the float one//
if ( intresult - tempresult < 0.000000000001 && intresult - tempresult > -0.000000000001 ){
result = intresult;
} else {
result = tempresult;
}

return result;
}

//example 1//
// defining first 500 terms of Euler number series//

var eulerNumber = [];
for(i=0; i<500; i++){
eulerNumber.push((1/factorial(i)));
}``````

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.