Skip to content

Commit b44b52f

Browse files
committed
stretch exercises solved
1 parent 29ef74a commit b44b52f

5 files changed

Lines changed: 155 additions & 9 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export function isCardValid(cardNum) {
2+
if (typeof cardNum !== "number") {
3+
return false;
4+
}
5+
const has16Length = String(cardNum).length === 16;
6+
const seenDigits = [];
7+
const isFinalEven = cardNum % 2 === 0;
8+
let total = 0;
9+
for (const num of String(cardNum)) {
10+
if (!seenDigits.includes(num)) {
11+
seenDigits.push(num);
12+
}
13+
const regex = /^\d$/;
14+
if (!regex.test(num)) {
15+
return false;
16+
}
17+
total += Number(num);
18+
}
19+
if (has16Length && isFinalEven && total > 16 && seenDigits.length >= 2) {
20+
return true;
21+
} else {
22+
return false;
23+
}
24+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// - Number must be 16 digits, all of them must be numbers.
2+
// - You must have at least two different digits represented (all of the digits cannot be the same).
3+
// - The final digit must be even.
4+
// - The sum of all the digits must be greater than 16.
5+
6+
import { isCardValid } from "./card-validator.js";
7+
8+
test("card number should have 16 digits", () => {
9+
const cardNumber = 1234567890987654;
10+
const result = isCardValid(cardNumber);
11+
expect(result).toEqual(true);
12+
});
13+
14+
test("card number should have at least two different digits, all of them cannot be the same", () => {
15+
const cardNumber = 1111111111111111;
16+
const result = isCardValid(cardNumber);
17+
expect(result).toEqual(false);
18+
});
19+
20+
test("final card digit must be even", () => {
21+
const cardNumber = 1111111111111118;
22+
const result = isCardValid(cardNumber);
23+
expect(result).toEqual(true);
24+
});
25+
26+
test("sum of all the digits must be greater than 16", () => {
27+
const cardNumber = 2222333344445554;
28+
const result = isCardValid(cardNumber);
29+
expect(result).toEqual(true);
30+
});
31+
32+
test("cardNumber must contains only numbers", () => {
33+
const cardNumber = "1233341234444444";
34+
const result = isCardValid(cardNumber);
35+
expect(result).toEqual(false);
36+
});
37+
38+
test("cardNumber sum is less than 16, should return false", () => {
39+
const cardNumber = 1111111111111110;
40+
const result = isCardValid(cardNumber);
41+
expect(result).toEqual(false);
42+
});
43+
44+
test("cardNumber uses invalid characters,should return false", () => {
45+
const cardNumber = "a92332119c011112";
46+
const result = isCardValid(cardNumber);
47+
expect(result).toEqual(false);
48+
});
49+
test("cardNumber uses only one type of number,should return false", () => {
50+
const cardNumber = 4444444444444444;
51+
const result = isCardValid(cardNumber);
52+
expect(result).toEqual(false);
53+
});
54+
55+
test("cardNumber has odo final number, should return false", () => {
56+
const cardNumber = 6666666666666661;
57+
const result = isCardValid(cardNumber);
58+
expect(result).toEqual(false);
59+
});

‎sprint-3-stretch-exercises/find.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ console.log(find("code your future", "z"));
2020
// Pay particular attention to the following:
2121

2222
// a) How the index variable updates during the call to find
23+
//index updates from value of 0 to increment value of +1 every time it loop through while loop if it reach length of str and won't find what it looks for function return -1
2324
// b) What is the if statement used to check
25+
//the if statement checks if str index character is at the same position as char index position we're looking for
2426
// c) Why is index++ being used?
27+
//every time our function loops through while statement index position increment by 1
2528
// d) What is the condition index < str.length used for?
29+
//while index is smaller than length of str the loop would run, if it reaches same value function return -1 instead
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
const passwords = [];
12
export function isValidPassword(password) {
2-
return password.length >= 5;
3+
let hasUpper = false;
4+
let hasLower = false;
5+
let hasNumbers = false;
6+
let hasSpecialChars = false;
7+
const minLength = password.length >= 5;
8+
const upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
9+
const lowerLetters = "abcdefghijklmnopqrstuvwxyz";
10+
const numbers = "0123456789";
11+
const specialChars = "!#$%.*&";
12+
13+
for (const char of password) {
14+
hasUpper = upperLetters.includes(char) ? true : hasUpper;
15+
hasLower = lowerLetters.includes(char) ? true : hasLower;
16+
hasNumbers = numbers.includes(char) ? true : hasNumbers;
17+
hasSpecialChars = specialChars.includes(char) ? true : hasSpecialChars;
18+
}
19+
if (
20+
minLength &&
21+
hasLower &&
22+
hasUpper &&
23+
hasNumbers &&
24+
hasSpecialChars &&
25+
!passwords.includes(password)
26+
) {
27+
passwords.push(password);
28+
return true;
29+
} else {
30+
return false;
31+
}
332
}

‎sprint-3-stretch-exercises/password-validator.test.js‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,41 @@ You must breakdown this problem in order to solve it. Find one test case first a
1616
*/
1717
import { isValidPassword } from "./password-validator.js";
1818
test("password has at least 5 characters", () => {
19-
// Arrange
20-
const password = "12345";
21-
// Act
22-
const result = isValidPassword(password);
23-
// Assert
24-
expect(result).toEqual(true);
25-
}
26-
);
19+
// Arrange
20+
const password = "Aa1!";
21+
// Act
22+
const result = isValidPassword(password);
23+
// Assert
24+
expect(result).toEqual(false);
25+
});
26+
27+
test("password have at least one uppercase letter(A-Z)", () => {
28+
const password = "hello123";
29+
const result = isValidPassword(password);
30+
expect(result).toEqual(false);
31+
});
32+
33+
test("password have at least one lowercase letter(a-z)", () => {
34+
const password = "A123AA";
35+
const result = isValidPassword(password);
36+
expect(result).toEqual(false);
37+
});
38+
39+
test("password have at least one number(0-9)", () => {
40+
const password = "RRRqqq";
41+
const result = isValidPassword(password);
42+
expect(result).toEqual(false);
43+
});
44+
45+
test("password have at least one special character(!#$%.*&)", () => {
46+
const password = "R3ass3";
47+
const result = isValidPassword(password);
48+
expect(result).toEqual(false);
49+
});
50+
51+
test("rejects a previously used password", () => {
52+
const password = "Hello123!";
53+
54+
expect(isValidPassword(password)).toEqual(true);
55+
expect(isValidPassword(password)).toEqual(false);
56+
});

0 commit comments

Comments
 (0)