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
46 changes: 46 additions & 0 deletions Sprint-3/3-stretch/credit-card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function validateCreditCard(cardNumber) {
const numStr = cardNumber.toString();

if (numStr.length !== 16) {
return false;
} // Number must be 16 digits.

const uniqueDigits = new Set(numStr);
if (uniqueDigits.size < 2) {
return false;
} // Must have at least two different digits.

const lastDigit = Number(numStr[numStr.length - 1]);
if (lastDigit % 2 !== 0) {
return false;
} // Last digit must be even.

let sum = 0;
for (let char of numStr) {
sum += Number(char);
} // Sum of all digits must be greater than 16.

if (sum <= 16) {
return false;
} // Sum check

return true;
}

// Test cases:

// Number must be 16 digits
console.log(validateCreditCard(9999)); // false
console.log(validateCreditCard(9999888877776662)); // true

// Must have at least two different digits
console.log(validateCreditCard(1111111111111111)); // false
console.log(validateCreditCard(1234567890123456)); // true

// Last digit must be even
console.log(validateCreditCard(1234567890123451)); // false
console.log(validateCreditCard(1234567890123452)); // true

// Sum of all digits must be greater than 16
console.log(validateCreditCard(1111111111111110)); // false
console.log(validateCreditCard(1234567890123456)); // true
Comment on lines +32 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also consider testing the boundary cases. e.g.,

  • sum of digits is 16
  • numbers with 15 digits
  • numbers with 2 distinct digits

7 changes: 7 additions & 0 deletions Sprint-3/3-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
// It keeps increasing by 1 until it finds the character or reaches the end of the string(the length of the string).

// b) What is the if statement used to check
// The if statement checks if the current character in the string is equal to the target character.

// c) Why is index++ being used?
// The index++ is used to move to the next character in the string during each iteration of the loop.

// d) What is the condition index < str.length used for?
// The condition index < str.length is used to ensure that the loop continues as long as there are characters left in the string to check.
39 changes: 36 additions & 3 deletions Sprint-3/3-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
// List of previously used passwords
const passwords = ["Abc!1", "Hello#2"];

function passwordValidator(password) {
return password.length < 5 ? false : true
}
// 1. Must have at least 5 characters
if (password.length < 5) {
return false;
}

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

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

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

// 5. Must have at least one symbol from the allowed list
if (!/[!#$%.*&]/.test(password)) {
return false;
}

// 6. Must not be a previously used password
if (passwords.includes(password)) {
return false;
}

// ✅ If all rules pass → valid
return true;
}

module.exports = passwordValidator;
module.exports = passwordValidator;
64 changes: 56 additions & 8 deletions Sprint-3/3-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,59 @@ You must breakdown this problem in order to solve it. Find one test case first a
*/
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 = "12345";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

// 2. Password is too short
test("password must be at least 5 characters", () => {
const password = "Ab1!";
const result = isValidPassword(password);
expect(result).toEqual(false);
});

// 3. Password must contain at least one uppercase letter
test("password must contain at least one uppercase letter", () => {
const password = "abc!1";
const result = isValidPassword(password);
expect(result).toEqual(false);
});

// 4. Password must contain at least one lowercase letter
test("password must contain at least one lowercase letter", () => {
const password = "ABC!1";
const result = isValidPassword(password);
expect(result).toEqual(false);
});

// 5. Password must contain at least one number
test("password must contain at least one number", () => {
const password = "Abc!@";
const result = isValidPassword(password);
expect(result).toEqual(false);
});

// 6. Password must contain at least one symbol (! # $ % . * &)
test("password must contain at least one symbol", () => {
const password = "Abc12";
const result = isValidPassword(password);
expect(result).toEqual(false);
});

// 7. Password must not be a previously used password
test("password must not be in the previous passwords array", () => {
const password = "Abc!1"; // this is in the passwords array inside the validator
const result = isValidPassword(password);
expect(result).toEqual(false);
});

// 8. Password is valid when all requirements are met
test("password is valid when it meets all requirements", () => {
const password = "Abc!2";
const result = isValidPassword(password);
expect(result).toEqual(true);
});