Skip to content

Commit 8037094

Browse files
authored
Create 2243-calculate-digit-sum-of-a-string.js
1 parent 114e705 commit 8037094

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {string}
5+
*/
6+
const digitSum = function(s, k) {
7+
let cur = s
8+
while(cur.length > k) {
9+
const arr = []
10+
for(let i = 0; i < cur.length; i += k) {
11+
let tmp = ''
12+
for(let j = 0; j < k && i + j < cur.length; j++) {
13+
tmp += cur[i + j]
14+
}
15+
arr.push(tmp)
16+
}
17+
arr.forEach((e, i) => {
18+
let res = 0
19+
for(let ch of e) res += +ch
20+
arr[i] = '' +res
21+
})
22+
cur = arr.join('')
23+
24+
}
25+
return cur
26+
};

0 commit comments

Comments
 (0)