This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathcard-validator.test.js
More file actions
73 lines (64 loc) · 1.91 KB
/
card-validator.test.js
File metadata and controls
73 lines (64 loc) · 1.91 KB
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
function cardValidator(cardNumber) {
let points = 0;
if (cardNumber.lenght === 0 && isNaN(cardNumber)) {
points--;
}
const container = "";
for (i = 0; i < cardNumber.lenght; i++) {
if (!container.includes(cardNumber[i])) {
container += cardNumber[i];
}
}
if (container < 2) {
points--;
}
if (Number(cardNumber.slice(-1)) % 2 === 1) {
points--;
}
const containerNum = 0;
for (i = 0; i < cardNumber.lenght; i++) {
containerNum += Number(cardNumber[i]);
}
if (containerNum < 17) {
points--;
}
if (points < 4) {
return false;
} else {
return true;
}
}
// - Number must be 16 digits, all of them must be numbers.
// - You must have at least two different digits represented (all of the digits cannot be the same).
// - The final digit must be even.
// - The sum of all the digits must be greater than 16.
test("Number must be 16 digits", function () {
const input = "123456789012345";
const currentOutput = cardValidator(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
test("all of them must be numbers", function () {
const input = "123456789012345a";
const currentOutput = cardValidator(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
test("at least two different digits", function () {
const input = "1111111111111111";
const currentOutput = cardValidator(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
test("The final digit must be even", function () {
const input = "1234567890123455";
const currentOutput = cardValidator(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
test("sum of all the digits must be greater than 16", function () {
const input = "1111111111111110";
const currentOutput = cardValidator(input);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});