
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Determining sum of array as even or odd in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of integers arr. Our function should return the string ‘odd’ if the sum of all the elements of the array is odd or ‘even’ if it’s even.
Example
Following is the code −
const arr = [5, 1, 8, 4, 6, 9]; const assignSum = (arr = []) => { const sum = arr.reduce((acc, val) => { return acc + val; }, 0); const isSumEven = sum % 2 === 0; return isSumEven ? 'even' : 'odd'; }; console.log(assignSum(arr));
Output
Following is the console output −
odd
- Related Questions & Answers
- Adding only odd or even numbers JavaScript
- Odd even sort in an array - JavaScript
- Odd even index difference - JavaScript
- Sum of individual even and odd digits in a string number using JavaScript
- Separate odd and even in JavaScript
- Total possible ways of making sum of odd even indices elements equal in array in \nJavaScript
- Splitting number to contain continuous odd or even number using JavaScript
- How to determine if a number is odd or even in JavaScript?
- Array Index with same count of even or odd numbers on both sides in C++
- Check if count of divisors is even or odd in Python
- Sum of even Fibonacci terms in JavaScript
- Even index sum in JavaScript
- Find even odd index digit difference - JavaScript
- Sorting odd and even elements separately JavaScript
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++
Advertisements