-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem2_2.js
64 lines (51 loc) · 1.58 KB
/
problem2_2.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
**Problem**
Write a function that can add a check digit to make the number valid per the LUhn formula
and return the original number plus that digit. This shoulld give
"2323 2005 7766 3554" in response to "2323 2005 7766 355"
Understanding the Problem
- input:
- string
- output:
- sting
- model of problem:
- pushing
**Examples / Test Cases**
**Data Structures**
**Algorithm**
git check sum
*/
function validLuhn(stringNum) {
stringNum = stringNum.replace(/\D/g, '');
let digits = stringNum.split('').reverse().map(str => Number(str));
let doubledDigits = digits.map((dig, index) => {
if (index % 2 === 0) {
return dig;
} else {
return dig * 2 < 10 ? (dig * 2) : ((dig * 2) - 9);
}
});
let checksum = doubledDigits.reduce((total, dig) => total + dig);
return checksum % 10 === 0;
}
function createChecksum(str) {
let validChecksum = validLuhn(str);
let newNumber = '';
let addNum = 0;
while(!validChecksum) {
if (validLuhn(str + String(addNum))) {
return str + String(addNum);
} else {
addNum += 1;
}
}
return str;
}
console.log(createChecksum("2323 2005 7766 355") === "2323 2005 7766 3554");
console.log(createChecksum('876') === '8763');
console.log(createChecksum('11111'));
console.log(createChecksum('8767'));
console.log(createChecksum('1111'));
console.log(createChecksum('2323az- 2005 pp7766[] 3554'));
console.log(createChecksum('2323az- 2005 pp7766[] 355'));
console.log(createChecksum('6541762821'));