Horje
Sum of Squares of Odd Numbers in an Array in JavaScript

The task involves iterating through the array, identifying odd numbers, squaring them, and summing up the squares. The different approaches to accomplish this task are listed below.

Iterative Approach

In this approach, we iterate through the array using a loop and for each element, check if it’s odd. If it is, we calculate its square and add it to the previously calculated square sum.

Example: The below code will find the sum of squares of odd numbers in an array using the iterative approach in JavaScript.

JavaScript
function sumOfSquaresOfOddNumbers(arr) {
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] % 2 !== 0) {
            sum += arr[i] * arr[i];
        }
    }
    return sum;
}

const numbers1 = [1, 2, 3, 4, 5];
const numbers2 = [2, 4, 6, 8, 10];
const numbers3 = [1, 3, 5, 7, 9];
console.log(sumOfSquaresOfOddNumbers(numbers1));
console.log(sumOfSquaresOfOddNumbers(numbers2));
console.log(sumOfSquaresOfOddNumbers(numbers3));

Output
35
0
165

Using Array Methods

In this approach, we use array methods like filter() to filter out odd numbers and reduce() to calculate the sum of squares.

Example: The below code find the sum of squares of odd numbers in an array using array methods in JavaScript.

JavaScript
function sumOfSquaresOfOddNumbers(arr) {
    return arr.filter(num => num % 2 !== 0).
        reduce((acc, curr) => acc + curr * curr, 0);
}

const numbers1 = [1, 2, 3, 4, 5];
const numbers2 = [2, 4, 6, 8, 10];
const numbers3 = [1, 3, 5, 7, 9];
console.log(sumOfSquaresOfOddNumbers(numbers1));
console.log(sumOfSquaresOfOddNumbers(numbers2));
console.log(sumOfSquaresOfOddNumbers(numbers3));

Output
35
0
165

Using map and reduce Methods

In this approach, we first use the map method to transform the array by squaring all the odd numbers and turning even numbers into zero. Then we use the reduce method to sum up all the squared values.

Example: The code below finds the sum of squares of odd numbers in an array using map and reduce methods in JavaScript. In this approach, map is used to square the odd numbers and replace even numbers with zero. The reduce method then calculates the sum of all the squared values. This approach ensures that we perform the squaring and summing operations in a clean and functional style.

JavaScript
function sumOfSquaresOfOddNumbers(arr) {
    return arr.map(num => num % 2 !== 0 ? num * num : 0)
              .reduce((acc, curr) => acc + curr, 0);
}

const numbers1 = [1, 2, 3, 4, 5];
const numbers2 = [2, 4, 6, 8, 10];
const numbers3 = [1, 3, 5, 7, 9];
console.log(sumOfSquaresOfOddNumbers(numbers1)); // Output: 35
console.log(sumOfSquaresOfOddNumbers(numbers2)); // Output: 0
console.log(sumOfSquaresOfOddNumbers(numbers3)); // Output: 165

Output
35
0
165






Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Create Enums in JavaScript ? How to Create Enums in JavaScript ?
How to Open a Popup on Hover using JavaScript ? How to Open a Popup on Hover using JavaScript ?
Find Volume of Cone using JavaScript Find Volume of Cone using JavaScript
JavaScript Program to Find Surface Area of a Cone JavaScript Program to Find Surface Area of a Cone
JavaScript if else else if JavaScript if else else if

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13