Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Sprint-3/3-stretch/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ console.log(find("code your future", "z"));
// Pay particular attention to the following:

// a) How the index variable updates during the call to find

// Starts at 0, increments by 1 each iteration (index++)
// Moves through each character position: 0 → 1 → 2 → 3 → etc.

// b) What is the if statement used to check

// Checks if the character at the current position (str[index]) matches the character we're searching for (char)

// c) Why is index++ being used?

// To move to the next character position in the string for the next iteration
// Without it, we'd have an infinite loop stuck at the same position

// d) What is the condition index < str.length used for?

// Ensures we don't go beyond the string length
// Prevents accessing invalid memory locations
// Stops the loop when we've checked all characters
29 changes: 27 additions & 2 deletions Sprint-3/3-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
}
// Check length (at least 5 characters)
if (password.length < 5) {
return false;
}

// Check for at least one uppercase letter (A-Z)
if (!/[A-Z]/.test(password)) {
return false;
}

// Check for at least one lowercase letter (a-z)
if (!/[a-z]/.test(password)) {
return false;
}

// Check for at least one number (0-9)
if (!/[0-9]/.test(password)) {
return false;
}

// Check for at least one special symbol: !, #, $, .
if (!/[!#$.]/.test(password)) {
return false;
}

// If all checks pass, return true
return true;
}

module.exports = passwordValidator;
41 changes: 33 additions & 8 deletions Sprint-3/3-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,38 @@ To be valid, a password must:

You must breakdown this problem in order to solve it. Find one test case first and get that working
*/

const isValidPassword = require("./password-validator");

test("password has at least 5 characters", () => {
// Arrange
const password = "12345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
);
// Arrange
const password = "Abc1!";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
});

test("password with less than 5 characters returns false", () => {
expect(isValidPassword("Ab1!")).toEqual(false);
});

test("password without uppercase returns false", () => {
expect(isValidPassword("abc1!")).toEqual(false);
});

test("password without lowercase returns false", () => {
expect(isValidPassword("ABC1!")).toEqual(false);
});

test("password without number returns false", () => {
expect(isValidPassword("Abcd!")).toEqual(false);
});

test("password without special symbol returns false", () => {
expect(isValidPassword("Abcd1")).toEqual(false);
});

test("valid password with all requirements returns true", () => {
expect(isValidPassword("Abc1!")).toEqual(true);
});