
- 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
Sum of individual even and odd digits in a string number using JavaScript
Problem
We are required to write a JavaScript function that takes in string containing digits and our function should return true if the sum of even digits is greater than that of odd digits, false otherwise.
Example
Following is the code −
const num = '645457345'; const isEvenGreater = (str = '') => { let evenSum = 0; let oddSum = 0; for(let i = 0; i < str.length; i++){ const el = +str[i]; if(el % 2 === 0){ evenSum += el; }else{ oddSum += el; }; }; return evenSum > oddSum; }; console.log(isEvenGreater(num));
Output
false
- Related Articles
- Find the sum of digits of a number at even and odd places in C++
- Count odd and even digits in a number in PL/SQL
- Number Split into individual digits in JavaScript
- Find the Number With Even Sum of Digits using C++
- Difference between sums of odd and even digits.
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Number of even substrings in a string of digits in C++
- Splitting number to contain continuous odd or even number using JavaScript
- How to split JavaScript Number into individual digits?
- Determining sum of array as even or odd in JavaScript
- Check if product of digits of a number at even and odd places is equal in Python
- Prime digits sum of a number in JavaScript
- Python program to find the sum of all even and odd digits of an integer list
- Fetch Numbers with Even Number of Digits JavaScript
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript

Advertisements