
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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
Advertisements