Sum of Squares of Odd Numbers in an Array in JavaScript Last Updated : 27 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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 ContentIterative ApproachUsing Array MethodsUsing map and reduce MethodsIterative 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. 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)); Output35 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. 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)); Output35 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. 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 Output35 0 165 Comment More infoAdvertise with us Next Article Sum of Squares of Odd Numbers in an Array in JavaScript A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Sum of Squares of Even Numbers in an Array using JavaScript JavaScript program allows us to compute the sum of squares of even numbers within a given array. The task involves iterating through the array, identifying even numbers, squaring them, and summing up the squares. There are several approaches to find the sum of the square of even numbers in an array 2 min read How to Find the Sum of an Array of Numbers in JavaScript ? To find the sum of an array of numbers in JavaScript, you can use several different methods.1. Using Array reduce() MethodThe array reduce() method can be used to find the sum of array. It executes the provided code for all the elements and returns a single output.Syntaxarr.reduce( callback( acc, el 2 min read Sum of Squares of First N Natural Numbers in JavaScript The sum of squares of the first n natural numbers is a mathematical problem of finding the sum of squares of each number up to a given positive number n. We can find the sum of squares of first n natural numbers in JavaScript using the approaches listed below.Table of Content Sum of Square of First 3 min read Print all Odd Numbers in a Range in JavaScript Array Odd numbers are numbers that cannot be divided into equal parts. If we divide the odd number by 2, the remainder is 1. In JavaScript, if we want to print all Odd numbers in a range, we can print it by iterating over the array, applying the condition, and printing the odd numbers. There are various a 4 min read JavaScript program to print even numbers in an array Given an array of numbers and the task is to write a JavaScript program to print all even numbers in that array. We will use the following methods to find even numbers in an array: Table of Content Method 1: Using for Loop Method 2: Using while Loop Method 3: Using forEach LoopMethod 4: Using filter 5 min read How to get the standard deviation of an array of numbers using JavaScript ? Given an array and the task is to calculate the standard deviation using JavaScript. Example: Input: [1, 2, 3, 4, 5]Output: 1.4142135623730951Input: [23, 4, 6, 457, 65, 7, 45, 8]Output: 145.13565852332775Please refer to Mean, Variance, and Standard Deviation for details. Mean is average of element. 3 min read Print all Even Numbers in a Range in JavaScript Array We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in 3 min read How to Add the Numbers in a JavaScript Array? Adding the numbers in a JavaScript array is a common operation, particularly in tasks involving data aggregation or mathematical calculations. This process involves iterating through the array and summing its elements. JavaScript provides multiple methods to achieve this, ranging from traditional lo 2 min read RMS Value Of Array in JavaScript The RMS (Root mean square) value of a distribution is the square root of the mean of the squares of the elements. The formula to find RMS value is given below: RMS = \sqrt{\frac{{a_{1}}^{2} + {a_{2}}^{2} + {a_{3}}^{2} + {a_{4}}^{2} + ... + {a_{N}}^{2}}{N}} To calculate the RMS value of an array, fir 3 min read JavaScript Program to Check if a Number is Odd or Even We are going to learn how to check whether the number is even or Odd using JavaScript. In the number system, any natural number that can be expressed in the form of (2n + 1) is called an odd number, and if the number can be expressed in the form of 2n is called an even number.Even = {2k : k â Z}(whe 3 min read Like