Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
Advertisements
