The following infinitely long series calculates the value of pi. pi = 4 - 4/3 +
ID: 3885542 • Letter: T
Question
The following infinitely long series calculates the value of pi. pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - ... Write a function named "piValue" which takes as input the number of terms in the series and returns the value of Ïeuro calculated using the above series. Note that the above series has five terms. Two example outputs of your function should be: piValue (6) = 2.9760 piValue (10) = 3.0418 Your function should work for any input value i.e. number of terms. SAVE Write your full name and student number as comments inside the function. Save your function with the above mentioned name.Explanation / Answer
function piValue(number_of_terms) {
var result = 0;
var j = -1;
for(var i = 1; i<= number_of_terms; i++) {
j += 2;
if(i % 2 == 0) {
result -= 4/j
}
else{
result += 4/j
}
}
return result;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.