-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetLucky.js
More file actions
39 lines (36 loc) · 768 Bytes
/
getLucky.js
File metadata and controls
39 lines (36 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var getLucky = function(s, k) {
let num = transform(s)
let res = 0
while(k > 0){
res = calculation(num)
k--
}
return res
};
const transform = s => {
const nums = new Array(s.length).fill(0)
for(let i = 0; i < s.length; i++){
nums[i] = s[i].charCodeAt() - 'a'.charCodeAt() + 1
}
let strOfNums = nums.join('')
// let ans = 0
// for(let i = 0; i < strOfNums.length; i++){
// ans += strOfNums[i] * 1
// }
// return
return strOfNums * 1
}
const calculation = num => {
let s = num + ''
let ans = 0
for(let i = 0; i < s.length; i++){
ans += s[i] * 1
}
return ans
}
getLucky('iiii',2)