Skip to content

Commit 4d308b3

Browse files
Completed Sprint-3/4-strecth
1 parent df245c8 commit 4d308b3

5 files changed

Lines changed: 72 additions & 6 deletions

File tree

Sprint-3/4-stretch/card-validator.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ In this project you'll write a script that validates whether or not a credit car
44

55
Here are the rules for a valid number:
66

7-
- Number must be 16 digits, all of them must be numbers.
8-
- You must have at least two different digits represented (all of the digits cannot be the same).
7+
- Number must be 16 digits, all of them must be numbers. checked
8+
- You must have at least two different digits represented (all of the digits cannot be the same).
99
- The final digit must be even.
1010
- The sum of all the digits must be greater than 16.
1111

@@ -33,3 +33,5 @@ These are the requirements your project needs to fulfill:
3333
- Return a boolean from the function to indicate whether the credit card number is valid.
3434

3535
Good luck!
36+
37+
function
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function isValidCreditCard(cardNumber) {
2+
// Rule 1: Must be exactly 16 characters long
3+
if (cardNumber.length !== 16) {
4+
return false;
5+
}
6+
7+
// Rule 2: Must contain only digits
8+
if (!cardNumber.match(/^\d{16}$/)) {
9+
return false;
10+
}
11+
12+
// Rule 3: Cannot be made up of only one repeated digit
13+
if (cardNumber.match(/^(.)\1+$/)) {
14+
return false;
15+
}
16+
17+
// Rule 4: Final digit must be even
18+
const lastDigit = Number(cardNumber[cardNumber.length - 1]);
19+
20+
if (lastDigit % 2 !== 0) {
21+
return false;
22+
}
23+
24+
// Rule 5: Sum of all digits must be greater than 16
25+
let sum = 0;
26+
27+
for (const digit of cardNumber) {
28+
sum += Number(digit);
29+
}
30+
31+
if (sum <= 16) {
32+
return false;
33+
}
34+
35+
// Passed every rule
36+
return true;
37+
}
38+
39+
module.exports = isValidCreditCard;

Sprint-3/4-stretch/find.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ console.log(find("code your future", "z"));
2121

2222
// a) How the index variable updates during the call to find
2323
// b) What is the if statement used to check
24+
// the if statement checks characters in the string
2425
// c) Why is index++ being used?
26+
// index++ is used to move to the next character in the string for the next iteration of the loop
2527
// d) What is the condition index < str.length used for?
28+
// index < str.length keeps the index within the string's valid range. If no match is found by the end, the function returns -1.
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
2+
if (password.length < 5) {
3+
return false;
4+
} else if (!/[A-Z]/.test(password)) {
5+
return false;
6+
} else if (!/[a-z]/.test(password)) {
7+
return false;
8+
} else if (!/[0-9]/.test(password)) {
9+
return false;
10+
} else if (!/[!#$%.*&]/.test(password)) {
11+
return false;
12+
} else {
13+
return true;
14+
}
15+
16+
317
}
418

519

6-
module.exports = passwordValidator;
20+
module.exports = passwordValidator;
21+
22+
// ("!", "#", "$", "%", ".", "*", "&")

Sprint-3/4-stretch/password-validator.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ You must breakdown this problem in order to solve it. Find one test case first a
1717
const isValidPassword = require("./password-validator");
1818
test("password has at least 5 characters", () => {
1919
// Arrange
20-
const password = "12345";
20+
const password = "2A4a#";
2121
// Act
2222
const result = isValidPassword(password);
2323
// Assert
2424
expect(result).toEqual(true);
2525
}
26-
);
26+
);
27+
28+
test("password must have an uppercase letter", () => {
29+
const password = "2a4a#";
30+
const result = isValidPassword(password);
31+
expect(result).toEqual(false);
32+
});

0 commit comments

Comments
 (0)