-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathluhn.ts
118 lines (103 loc) · 2.85 KB
/
luhn.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 1. reverse the order of the digits
// 2. s1 = sum of all the odd digits (space 1 + space 3 + ...)
// 3. Take the even digits, multiply each digit by 2, sum the 2 digits if answer is >9
// 4. s2 = sum of all the even digits
// 5. If s1 + s2 ENDS in 0, then the number passes the Luhn Test
//Step 1
var convertString = function(param1: number): string {
return param1.toString();
};
var convertNumber = function(param2: string): number {
return Number(param2);
};
var reverseOrder = function(param: number): string {
var createString = convertString(param);
var stringLength = createString.length;
var result = "";
var result2 = "";
var finalResult = "";
for (var i = 0; i < createString.length; i++) {
result = createString[i];
result2 = createString[stringLength - 1];
result = result2;
finalResult += result;
stringLength--;
}
return finalResult;
};
var isEven = function(num: number): boolean {
return num % 2 === 0;
};
var isOdd = function(num: number): boolean {
return num % 2 !== 0;
};
var pickEven = function(param: string): string {
var result = "";
var finalResult = "";
for (var i = 1; i < param.length; i += 2) {
if (convertNumber(param[i])) {
result = param[i];
finalResult += result;
}
}
return finalResult;
};
var pickOdd = function(param: string): string {
var result = "";
var finalResult = "";
for (var i = 0; i < param.length; i += 2) {
if (convertNumber(param[i])) {
result = param[i];
finalResult += result;
}
}
return finalResult;
};
var sumOdd = function(word: string): number {
var result = 0;
var finalResult = 0;
for (var i = 0; i < word.length; i++) {
result = convertNumber(word[i]);
finalResult += result;
}
return finalResult;
};
var sumEven = function(word: string): number {
var result = 0;
var finalResult = 0;
for (var i = 0; i < word.length; i++) {
result = convertNumber(word[i]) * 2;
if (result > 9) {
var changeString = convertString(result);
var final = 0;
for (var j = 0; j < changeString.length; j++) {
result = convertNumber(changeString[j]);
final += result;
result = 0;
}
finalResult += final;
}
finalResult += result;
}
return finalResult;
};
var example = 1234567812345670;
var s1 = sumOdd(pickOdd(reverseOrder(example)));
var s2 = sumEven(pickEven(reverseOrder(example)));
var totalSum = s2 + s1;
var stringForm = convertString(totalSum);
var lastNumber = convertNumber(stringForm[stringForm.length - 1]);
if (lastNumber === 0){
console.log("You have a VALID Credit Card!")
} else {
console.log("Go away you fraud.")
}
//console.log(s1);
//console.log(s2);
console.log(totalSum);
//var solution = (reverseOrder(123456789));
//if (typeof solution === 'number') {
// console.log("You are amazing")
//} else {
// console.log("Nope.")
//}