-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
130 lines (115 loc) · 3.79 KB
/
script.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
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
119
120
121
122
123
124
125
126
127
128
129
130
// capture input from user
const button = document.querySelector(".btn");
const regex = /^[0-9]+$/;
const result = document.querySelector("#result");
const input = document.querySelector("#cardNumber");
// Check button
button.addEventListener("click", function (event) {
event.preventDefault();
let user_input = input.value; //document.cardForm.cardNumber.value
// check if input is number + prevent empty string in input.
if (!user_input.match(regex)) {
console.warn("Wrong input type");
alert("Must input numbers");
} else {
let output = cardCheck(user_input);
// return the result var to HTML using inner.HTML
result.innerHTML = output;
}
});
// Clear button
const clear_button = document.querySelector(".clear");
clear_button.addEventListener("click", function (event) {
event.preventDefault();
result.innerHTML = "";
input.value = "";
});
function cardCheck(user_input) {
const l = user_input.length;
// find the digits backward starting from the second-to-last digit
let arr_reverse_0 = [];
let counter_1 = 0;
for (let i = l - 2; i >= 0; i -= 2) {
arr_reverse_0[counter_1] = user_input[i];
counter_1++;
}
// console.log(`arr reverse: ${arr_reverse_0}`);
// store the rest of the digits
let arr_0 = [];
let counter_0 = 0;
for (let x = l - 1; x >= 0; x -= 2) {
arr_0[counter_0] = parseInt(user_input[x], 10);
counter_0++;
}
// console.log(`arr: ${arr_0}`);
// multiply every other digit in arr_reverse_0 by 2. store in arr_reverse_1
let arr_reverse_multiply = [];
for (let j = 0; j < arr_reverse_0.length; j++) {
arr_reverse_multiply[j] = arr_reverse_0[j] * 2;
}
// then add those products’ digits together (not the products itself)
let sum_0 = 0;
for (let k = 0; k < arr_reverse_multiply.length; k++) {
if (arr_reverse_multiply[k] < 10) {
sum_0 = sum_0 + arr_reverse_multiply[k];
} else {
sum_0 = sum_0 + 1 + parseInt([arr_reverse_multiply[k] % 10], 10); // without parseInt, error. Why?
}
}
// console.log(`sum_0: ${sum_0}`);
// Add sum_0 with the sum of the digits that weren’t multiplied by 2
const reducer = (acc, cur) => acc + cur;
const sum_of_arr_0 = arr_0.reduce(reducer);
const final_sum = sum_0 + sum_of_arr_0;
// console.log(`final sum: ${final_sum}`);
// check if the card is legit
if (final_sum % 10 !== 0) {
return "INVALID";
} else {
let first_e = user_input[0];
let first_2e = user_input[0] + user_input[1];
if (l === 13 && first_e.localeCompare("4") === 0) {
return "VISA";
} else if (l === 16 && first_e.localeCompare("4") === 0) {
return "VISA";
} else if (
l === 16 &&
(first_2e.localeCompare("51") === 0 ||
first_2e.localeCompare("52") === 0 ||
first_2e.localeCompare("53") === 0 ||
first_2e.localeCompare("54") === 0 ||
first_2e.localeCompare("55") === 0)
) {
return "MASTERCARD";
} else if (
l === 15 &&
(first_2e.localeCompare("34") === 0 || first_2e.localeCompare("37") === 0)
) {
return "AMEX";
} else {
return "INVALID";
}
}
}
// --------- NOTE ---------
/* check if input is number
Failed: Use type="number" in HTML will create an arrow up and down to change number. It will affect the card number
Test case passed: 5h, h, 55, ,
Test case failed: 10.5
if (isNaN(user_input) || user_input === "" || user_input ==null) {
alert("Must input numbers");
return false;
} else {
console.log(user_input);
} */
// Check the length of input. Done by maxlength in HTML
// // create reference object
// let dict = {};
// dict[4] = "VISA";
// dict[51] = "MASTERCARD";
// dict[52] = "MASTERCARD";
// dict[53] = "MASTERCARD";
// dict[54] = "MASTERCARD";
// dict[55] = "MASTERCARD";
// dict[34] = "AMEX";
// dict[37] = "AMEX";