Squared sum of n odd numbers - JavaScript


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

Updated on: 15-Sep-2020

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements