-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.js
39 lines (30 loc) · 878 Bytes
/
exercise.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
31
32
33
34
35
36
37
38
39
var ccNums = ['4400016049422110',
'4485633318676001',
'4532617069310928',
'5230851628590439',
'5277370710835083',
'5409165634150103',
'340834486976393',
'377537528407480',
'346868166309859',
'30099784180511',
'30451460601877',
'30129124863771'].forEach(function(ccNum) {
// console.log(ccNum);
const newArr = ccNum.split('').reverse().map(function(val, index) {
// console.log('index: %s, odd? %s',index, index % 2 !== 0);
if(index % 2 !== 0) {
val = val * 2;
if(val > 9) {
val = val.toString().split('').reduce(function (prev, curr) {
return parseInt(prev, 10) + parseInt(curr, 10);
});
}
}
return val;
});
var checkSum = newArr.reverse().reduce(function (prev, curr) {
return parseInt(prev, 10) + parseInt(curr, 10);
});
console.log('ccNum: %s, checkSum: %s', ccNum, checkSum * 9 % 10);
});