-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharCodeCalc.js
30 lines (26 loc) · 902 Bytes
/
charCodeCalc.js
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
// 'ABC' --> 'A' = 65, 'B' = 66, 'C' = 67 --> 656667
// Then replace any incidence of the number 7 with the number 1, and call this number 'total2':
// total1 = 656667
// ^
// total2 = 656661
// ^
// Then return the difference between the sum of the digits in total1 and total2:
// (6 + 5 + 6 + 6 + 6 + 7)
// - (6 + 5 + 6 + 6 + 6 + 1)
// -------------------------
// 6
function calc(x){
let total1 = x.split("").map(elem => elem.charCodeAt()).join("")
let total2 = total1.split("").map(elem => elem === "7" ? 1:elem)
return total1.split("").reduce((a,b)=> a + Number(b),0) - total2.reduce((a,b)=> a + Number(b),0)
}
// function calc(x){
// return x
// .split('')
// .map(c => c.charCodeAt(0))
// .join('')
// .split('')
// .map(Number)
// .filter(x => x === 7)
// .length * 6
// }