LeetCode: Sum of Digits of String After Convert Solution

Solving a problem in one go is such a good feeling

Implementation

1var getLucky = function (s, k) {
2 let transform = s
3 .split("")
4 .map(char => char.charCodeAt(0) - "a".charCodeAt(0) + 1)
5 .join("")
6
7 while (k--) {
8 transform = transform
9 .split("")
10 .map(Number)
11 .reduce((acc, el) => acc + el, 0)
12 .toString()
13 }
14
15 return transform
16}

References

Original problem

Similar problems

Happy Number

Add Digits

Count Integers With Even Digit Sum

Comments

Loading comments...

Tags

leetcode

string

math

Next Post

CodeWars: Human readable duration format

Readability rather than trying to be clever

Previous Post

LeetCode: Add Digits

Could work with string instead of using math

HoningJS

Search Posts