how to make a function that would calculate the sum of the five terms of an arithmetic progression, but the parameters of the function – the initial term and the difference – are entered by the user. I tried this but i have no idea what to do
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
function progress(){
var i, x = 0;
for (i = 1; i <= 5; i )
{x = i}
}
</script>
</body>
CodePudding user response:
The following function will work.
function calculate(initialTerm , diff) // 1 , 1
{
let sum = 0;
let term = initialTerm; //1
for(let i = 0; i < 5; i )
{
sum =term; //1 3 6 10 15
term =diff; // 2 3 4 5
}
console.log(sum);
}
CodePudding user response:
The sum of first n terms in an AP is given by - n/2*[2a (n-1)d]
where n is the number of terms, a is initial value and d is the difference.
So you can use the following function -
function progress(a, d) {
return 5/2*[2*a 4 * d];
}