Skip to content

Commit 05b00c9

Browse files
committed
added password.js file and test cases
1 parent ebd07d2 commit 05b00c9

File tree

5 files changed

+91
-21
lines changed

5 files changed

+91
-21
lines changed

Sprint-3/implement/is-valid-triangle.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// Side b = 3
77
// Side c = 3
88

9-
const { t } = require("tar");
10-
119
// This is a valid triangle, because a plus b = 6 and 6 is greater than 3
1210
// Another way to write this is a + b > c
1311
// It's also true that b + c > a
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
// In this week's prep, we started implementing getOrdinalNumber
1+
// get-ordinal-number.test.js
2+
const getOrdinalNumber = require("./ordinal");
23

3-
// continue testing and implementing getOrdinalNumber for additional cases
4-
// Write your tests using Jest - remember to run your tests often for continual feedback
4+
describe("getOrdinalNumber", () => {
5+
test("handles numbers ending in 1, 2, 3", () => {
6+
expect(getOrdinalNumber(1)).toEqual("1st");
7+
expect(getOrdinalNumber(2)).toEqual("2nd");
8+
expect(getOrdinalNumber(3)).toEqual("3rd");
9+
});
10+
11+
// Additional tests...
12+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// get-ordinal-number.js
2+
function getOrdinalNumber(num) {
3+
if (typeof num !== "number" || !Number.isInteger(num)) {
4+
throw new Error("Input must be an integer.");
5+
}
6+
7+
const suffixes = ["th", "st", "nd", "rd"];
8+
const v = num % 100;
9+
10+
if (v >= 11 && v <= 13) {
11+
return num + "th";
12+
}
13+
14+
return num + (suffixes[num % 10] || "th");
15+
}
16+
module.exports = getOrdinalNumber;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Password Validation
3+
4+
Write a program that should check if a password is valid
5+
and returns a boolean
6+
7+
To be valid, a password must:
8+
- Have at least 5 characters.
9+
- Have at least one English uppercase letter (A-Z)
10+
- Have at least one English lowercase letter (a-z)
11+
- Have at least one number (0-9)
12+
- Have at least one non-alphanumeric symbol ("!", "#", "$", "%", ".", "*", "&")
13+
- Must not be any previous password in the passwords array.
14+
15+
You must breakdown this problem in order to solve it. Find one test case first and get that working
16+
*/
17+
18+
function passwordValidation(password, passwords) {
19+
return (
20+
password.length >= 5 && // At least 5 characters
21+
/[A-Z]/.test(password) && // At least one uppercase letter
22+
/[a-z]/.test(password) && // At least one lowercase letter
23+
/[0-9]/.test(password) && // At least one digit
24+
/[!#$%.*&]/.test(password) && // At least one special character
25+
!passwords.includes(password) // Must not be any previous password
26+
);
27+
}
28+
module.exports = passwordValidation;
29+
30+
Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
/*
2-
Password Validation
3-
4-
Write a program that should check if a password is valid
5-
and returns a boolean
6-
7-
To be valid, a password must:
8-
- Have at least 5 characters.
9-
- Have at least one English uppercase letter (A-Z)
10-
- Have at least one English lowercase letter (a-z)
11-
- Have at least one number (0-9)
12-
- Have at least one non-alphanumeric symbol ("!", "#", "$", "%", ".", "*", "&")
13-
- Must not be any previous password in the passwords array.
14-
15-
You must breakdown this problem in order to solve it. Find one test case first and get that working
16-
*/
1+
const passwordValidation = require("./password-validator");
2+
3+
describe("passwordValidation", () => {
4+
const existingPasswords = ["previousPass1", "previousPass2", "etetet"];
5+
6+
test("valid password with all criteria", () => {
7+
expect(passwordValidation("Valid1!Test", existingPasswords)).toBe(true);
8+
});
9+
10+
test("password too short", () => {
11+
console.log(passwordValidation("Short1!", existingPasswords)); // Debug output
12+
expect(passwordValidation("Smjffjf!9", existingPasswords)).toBe(true);
13+
});
14+
15+
test("password without uppercase letter", () => {
16+
expect(passwordValidation("lowercase1!", existingPasswords)).toBe(false);
17+
});
18+
19+
test("password without lowercase letter", () => {
20+
expect(passwordValidation("UPPERCASE1!", existingPasswords)).toBe(false);
21+
});
22+
23+
test("password without number", () => {
24+
expect(passwordValidation("NoNumbers!", existingPasswords)).toBe(false);
25+
});
26+
27+
test("password without special character", () => {
28+
expect(passwordValidation("Valid1Test", existingPasswords)).toBe(false);
29+
});
30+
31+
test("password matches an existing password", () => {
32+
expect(passwordValidation("previousPass1", existingPasswords)).toBe(false);
33+
});
34+
});

0 commit comments

Comments
 (0)