
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Adding only odd or even numbers JavaScript
We are required to make a function that given an array of numbers and a string that can take any of the two values “odd” or “even”, adds the numbers which match that condition. If no values match the condition, 0 should be returned.
For example −
console.log(conditionalSum([1, 2, 3, 4, 5], "even")); => 6 console.log(conditionalSum([1, 2, 3, 4, 5], "odd")); => 9 console.log(conditionalSum([13, 88, 12, 44, 99], "even")); => 144 console.log(conditionalSum([], "odd")); => 0
So, let’s write the code for this function, we will use the Array.prototype.reduce() method here −
Example
const conditionalSum = (arr, condition) => { const add = (num1, num2) => { if(condition === 'even' && num2 % 2 === 0){ return num1 + num2; } if(condition === 'odd' && num2 % 2 === 1){ return num1 + num2; }; return num1; } return arr.reduce((acc, val) => add(acc, val), 0); } console.log(conditionalSum([1, 2, 3, 4, 5], "even")); console.log(conditionalSum([1, 2, 3, 4, 5], "odd")); console.log(conditionalSum([13, 88, 12, 44, 99], "even")); console.log(conditionalSum([], "odd"));
Output
The output in the console will be −
6 9 144 0
- Related Articles
- Finding the only even or the only odd number in a string of space separated numbers in JavaScript
- How To Do Divisibility In Odd Or Even Numbers
- Even numbers at even index and odd numbers at odd index in C++
- The sum of two prime numbers $(>2)$ is(A) odd (B) even (C) prime (D) even or odd
- State whether the following statements are True or False:(a) The sum of three odd numbers is even.(b) The sum of two odd numbers and one even number is even.(c) The product of three odd numbers is odd.(d) If an even number is divided by 2, the quotient is always odd.(e) All prime numbers are odd.(f) Prime numbers do not have any factors.(g) Sum of two prime numbers is always even.(h) 2 is the only even prime number.(i) All even numbers are composite numbers.(j) The product of two even numbers is always even.
- Repeating only even numbers inside an array in JavaScript
- Determining sum of array as even or odd in JavaScript
- What are even and odd numbers?
- Odd even index difference - JavaScript
- Splitting number to contain continuous odd or even number using JavaScript
- Separate odd and even in JavaScript
- Design a DFA machine accepting odd numbers of 0’s or even numbers of 1’s
- How to determine if a number is odd or even in JavaScript?
- Check whether product of 'n' numbers is even or odd in Python
- What are prime numbers and composite numbers And how we can know thank the prime or composite numbers are odd or even?

Advertisements