- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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