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
18 changes: 17 additions & 1 deletion Sprint-3/3-stretch/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@ console.log(find("code your future", "z"));
// Use the Python Visualiser to help you play computer with this example and observe how this code is executed
// Pay particular attention to the following:

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

// As the code runs, index starts from zero and runs until the length of the string,
// It starts from zero and in each loop it gets incremented until a char is found within the string.

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

// It checks if the char is in the string. If it exists.. next step is to return the index of the char.


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

// To check if the given char is present starting from 0 till str.length.


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

// As long as index is less than the length of the string, keep looping until the char is found.

// In find.js explanations given to the question.
15 changes: 12 additions & 3 deletions Sprint-3/3-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
function isValidPassword(password, previousPasswords = []) {
return (
password.length >= 5 &&
/[A-Z]/.test(password) &&
/[a-z]/.test(password) &&
/[0-9]/.test(password) &&
/[!#$%.*&]/.test(password) &&
!previousPasswords.includes(password)
);
}


module.exports = passwordValidator;
module.exports = isValidPassword;

// In password-validator.js function implemented.
49 changes: 41 additions & 8 deletions Sprint-3/3-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,45 @@ 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");
const previousPasswords = ["Mmd1!", "XyZ2$", "Tes5%"];


test("password has at least 5 characters", () => {
// Arrange
const password = "12345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
);
const password = "Ki55$";
const result = isValidPassword(password, previousPasswords); // pass the array
expect(result).toEqual(true);
});

test("password has at least one uppercase", () => {
const password = "Uo85*";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(true);npx
});

test("password has at least one lowercase", () => {
const password = "Qf#45";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(true);
});

test("password has at least one number", () => {
const password = "Cz!35";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(true);
});

test("password has at least one special symbol", () => {
const password = "Re*19";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(true);
});
Comment on lines +27 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For each of these test case, we should also test if the function can correctly return false when a password meets all criteria except the one specified in the test description.


test("password must not be a previous password", () => {
const password = "Mmd1!";
const result = isValidPassword(password, previousPasswords);
expect(result).toEqual(false);

});


// In Password-validator.test.js cases tested and passed.