![]() |
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. Table of Content Iterative ApproachIn 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.
Output 35 0 165 Using Array MethodsIn 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.
Output 35 0 165 Using map and reduce MethodsIn 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.
Output 35 0 165 |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |