LeetCode: Count Integers With Even Digit Sum Solution

Map and filter

Approach

Populate new array with digit sum

Filter even elements

Implementation

1var countEven = function (num) {
2 const digitSum = num =>
3 num
4 .toString()
5 .split("")
6 .map(Number)
7 .reduce((acc, el) => acc + el, 0)
8
9 const isEven = num => num % 2 === 0
10
11 return Array.from({ length: num }, (_, i) => i + 1)
12 .map(digitSum)
13 .filter(isEven).length
14}

References

Original problem

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

Next Post

LeetCode: Special Positions in a Binary Matrix

Count number of 1s

HoningJS

Search Posts