LeetCode: Count Integers With Even Digit Sum Solution
Map and filterApproach
Populate new array with digit sum
Filter even elements
Implementation
1var countEven = function (num) {2 const digitSum = num =>3 num4 .toString()5 .split("")6 .map(Number)7 .reduce((acc, el) => acc + el, 0)89 const isEven = num => num % 2 === 01011 return Array.from({ length: num }, (_, i) => i + 1)12 .map(digitSum)13 .filter(isEven).length14}
References
Similar problems
Sum of Numbers With Units Digit K
Sum of Digits of String After Convert
Number of Ways to Buy Pens and Pencils
Comments
Loading comments...
Tags
leetcode
math
string
array
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Special Positions in a Binary Matrix
Count number of 1s