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
43 changes: 43 additions & 0 deletions Sprint-3/4-stretch/Credit-Card-Validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function validateCreditCard(cardNumber) {
if (cardNumber.length !== 16) {
return false; // this line of code checks whether the card number is 16 digits long or not
}
let index = 0;
while (index < cardNumber.length) {
if (cardNumber[index] < "0" || cardNumber[index] > "9") {
return false; // this line of code checks whether the card number is composed of digits not other characters
}
index++;
}
const firstDigit = cardNumber[0]; // first digit of the card number is stored for the purpose of checking up whether two digits are identical or not

index = 1;
while (index < cardNumber.length) {
if (cardNumber[index] !== firstDigit) {
break; // loop ends immediately here to give way to the next check
}
index++;
}
if (index === cardNumber.length) {
return false;
}
const lastDigit = cardNumber.slice(-1);
const lastNumber = Number(lastDigit);

if (lastNumber % 2 !== 0) {
return false; // this line of code checks whether the last digit of the card number is divisible by 2 in other words "even" or not
}

let total = 0;
index = 0;
while (index < cardNumber.length) {
total += Number(cardNumber[index]);
index++;
}
if (total <= 16) {
return false; // this line of code checks whether the sum of all the digits in the card number is more than 16 or not
}

return true;
}
console.log(validateCreditCard("9999777788880000"));
7 changes: 7 additions & 0 deletions Sprint-3/4-stretch/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ console.log(find("code your future", "z"));
// Pay particular attention to the following:

// a) How the index variable updates during the call to find
// During the call to find(str, char): index starts at 0 and on each loop iteration, the function checks if str[index] is equal to char. if it is not, index is incremented by 1 (index++) and the loop continues until either a match is found or index reaches str.length. If a match is found, the function returns the current value of index. If no match is found after checking all characters, the function returns -1.
// C O D E Y O "U" R F U T U R E
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 => MATCH FOUND --> return index = 7

// b) What is the if statement used to check
// The if statement checks if str[index] is equal to char. This implies whether the current character in the string matches the character we are searching for. If it does, the function returns the current index. If not, the loop continues to the next character.
// c) Why is index++ being used?
// index++ is used to increment the index variable by 1 after each loop iteration and this is done to move to the next character in the string for comparison with char. This enables the function to check each character in the string sequentially until a match is found or the end of the string is reached.
// d) What is the condition index < str.length used for?
// The condition index < str.length is used to ensure that the loop continues to run as long as the index is less than the length of the string. This prevents the function from trying to run indefinitely and ensures that it only checks valid indices within the bonds of a given string. Therefore, the loop will terminate when index reaches str.length, indicating that all characters have been checked without finding a match.
28 changes: 25 additions & 3 deletions Sprint-3/4-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
// must be at least 5 characters long
if (password.length < 5) {
return false;
}
// must contain at least one lowercase letter
const hasLowercase = /[a-z]/.test(password);
// must contain at least one uppercase letter
const hasUppercase = /[A-Z]/.test(password);
// must contain at least one digit
const hasDigit = /[0-9]/.test(password);
// must contain at least one special character
const hasSpecialChar = /[!@#$%^&*()\-+]/.test(password);
// must not contain any spaces
const hasNoSpaces = !/\s/.test(password);
// must not be any previous password in the passwords array
const isNewPassword = !previouspasswords.includes(password);
return (
hasLowercase &&
hasUppercase &&
hasDigit &&
hasSpecialChar &&
hasNoSpaces &&
isNewPassword
);
}


module.exports = passwordValidator;
module.exports = passwordValidator;
49 changes: 49 additions & 0 deletions Sprint-3/4-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ To be valid, a password must:
- Have at least one number (0-9)
- Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&")
- Must not be any previous password in the passwords array.
-Must not contain any spaces.

You must breakdown this problem in order to solve it. Find one test case first and get that working
*/
Expand All @@ -23,4 +24,52 @@ test("password has at least 5 characters", () => {
// Assert
expect(result).toEqual(true);
}
test("password has at least one English uppercase letter (A-Z)", () => {
// Arrange
const password = "1A345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
test("password has at least one English lowercase letter (a-z)", () => {
// Arrange
const password = "1a345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
test("password has at least one number (0-9)", () => {
// Arrange
const password = "Abcd5";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
test("password has at least one special character", () => {
// Arrange
const password = "1234!";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
}
test("password has no spaces", () => {
// Arrange
const password = "Abc 5$";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
}
test("password is not a previous password", () => {
// Arrange
const password = "12345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
}
);
Loading