Squared sum of n odd numbers - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in a Number, say n, and finds the sum of the square of first n odd natural Numbers.

For example −

If the input number is 3. Then the output will be −

1^2 + 3^2 + 5^2 = 35

Therefore, the output is −

35

Example

Following is the code −

const num = 3;
const squaredSum = num => {
   let sum = 0;
   for(let i = 1; i <= num; i++){
      sum += Math.pow((2 * i) - 1, 2);
   };
   return sum;
};
console.log(squaredSum(num));

Output

Following is the output in the console −

35
raja
Published on 15-Sep-2020 15:13:14
Advertisements