Skip to content

Commit 0851cad

Browse files
authored
Create 1945-sum-of-digits-of-string-after-convert.js
1 parent 6fb3bb5 commit 0851cad

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const getLucky = function(s, k) {
7+
let res = 0
8+
const a = 'a'.charCodeAt(0)
9+
const arr = []
10+
for(let ch of s) {
11+
arr.push(ch.charCodeAt(0) - a + 1)
12+
}
13+
let str = arr.join('').split('').map(e => +e)
14+
let prev = str, sum = 0
15+
while(k > 0) {
16+
// let tmp = 0
17+
let tmp = prev.reduce((ac, e) => ac + e, 0)
18+
// console.log(tmp)
19+
prev = `${tmp}`.split('').map(e => +e)
20+
sum = tmp
21+
k--
22+
}
23+
24+
return sum
25+
};

0 commit comments

Comments
 (0)