Skip to content
Open
38 changes: 38 additions & 0 deletions Sprint-3/4-stretch/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function validateCreditCardNumber(cardNumber) {
// Check if the card number is a positive integer.
if (typeof cardNumber !== "number" || cardNumber < 0) {
return false;
}
Comment on lines +2 to +5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note:

  • To check if a value is in integer, Number.isInteger() is better.

  • Credit card numbers typically have 16 digits, but not all 16-digit integers can be safely represented as a number in JS -- many numbers larger than Number.MAX_SAFE_INTEGER cannot be represented properly. For example, 9007199254740993. Normally, card numbers are represented as strings.

No change required.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

cool observation. thanks


const cardNumberString = String(cardNumber);
// Check if the card number has at least two different digits
const uniqueDigits = new Set(cardNumberString);

if (uniqueDigits.size < 2) {
return false;
}

// Check if the card number has exactly 16 digits.
if (cardNumberString.length !== 16) {
return false;
}

// Check if the last digit is even
if (Number(cardNumberString.slice(-1)) % 2 !== 0) {
return false;
}

// Check if the sum of all the digits is greater than 16
const sumOfAllDigits = Array.from(cardNumberString).reduce(
(sum, digit) => sum + Number(digit),
0
);

if (sumOfAllDigits <= 16) {
return false;
}

return true;
}

module.exports = validateCreditCardNumber;
91 changes: 91 additions & 0 deletions Sprint-3/4-stretch/card-validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const validateCreditCardNumber = require("./card-validator");

test("Number must be 16 digits, all of them must be numbers.", () => {
// Arrange
const cardNumber = 1234567890123456;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(true);
});

test("Credit card number must have at least two different digits.", () => {
// Arrange
const cardNumber = 6262826262628262;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(true);
});

test("Credit card with only one repeating digit is invalid.", () => {
// Arrange
const cardNumber = 8888888888888888;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(false);
});

test("Credit card number cannot have an odd last digit.", () => {
// Arrange
const cardNumber = 1234567890123457;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(false);
});

test("Credit card number sum of all digits cannot be less than or equal to 16.", () => {
// Arrange
const cardNumber = 1000000000000000;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(false);
});

test("Credit card number sum of all digits must be greater than 16.", () => {
// Arrange
const cardNumber = 1234567890123452;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(true);
});

test("Credit card number must not be negative.", () => {
// Arrange
const cardNumber = -1234567890123456;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(false);
});

test("Credit card number must be a number.", () => {
// Arrange
const cardNumber = "1234567890123456";
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(false);
});

test("Credit card number must have exactly 16 digits.", () => {
// Arrange
const cardNumber = 212222222212222;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(false);
});

test("Credit card number cannot have less or more than 16 digits.", () => {
// Arrange
const cardNumber = 212222222212222;
// Act
const result = validateCreditCardNumber(cardNumber);
// Assert
expect(result).toEqual(false);
});
13 changes: 13 additions & 0 deletions Sprint-3/4-stretch/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ console.log(find("code your future", "z"));
// Pay particular attention to the following:

// a) How the index variable updates during the call to find
// The index variable starts at 0 and increments by 1 in each iteration of the while loop.
// In case of the call find("code your future", "u"), the index variable updates as follows:
// 0, 1, 2, 3, 4, 5, 6, 7 - when it stops, because the character at index 7 is "u"
// In case of the call find("code your future", "z"), the index variable updates as follows:
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 - when it stops, because the condition index < str.length is no longer true
// the and because the loop has arrived to the end of the string without finding "z".
Comment thread
cjyuan marked this conversation as resolved.

// b) What is the if statement used to check
// The if statement is used to check if the character at the current index is equal to the character we are looking for.

// c) Why is index++ being used?
// index++ is incrementing the index by 1 in each iteration of the while loop. this is how the index is being moved from 0 to the end of the string, or the index where the character is found.

// d) What is the condition index < str.length used for?
// It gives the limit to the looping, that is, the index must be less than the length of the string, and in case the index is equal or greater than the string length, the loop will stop.
// If this condition wasn't given, the loop would continue to run indefinitely.
28 changes: 24 additions & 4 deletions Sprint-3/4-stretch/password-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
function passwordValidator(password) {
return password.length < 5 ? false : true
function passwordValidator(
password,
usedPasswords = ["pa$$w0rd", "Qwerty1#", "Adm1n2#", "$3cr4t"]
) {
/* To be valid, a password must:
- Have at least 5 characters.
- Have at least one English uppercase letter (A-Z)
- Have at least one English lowercase letter (a-z)
- 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.
*/
if (
password.length < 5 ||
!/[A-Z]/.test(password) ||
!/[a-z]/.test(password) ||
!/[0-9]/.test(password) ||
!/[!#$%.*&]/.test(password) ||
usedPasswords.includes(password)
) {
return false;
}
return true;
}


module.exports = passwordValidator;
module.exports = passwordValidator;
69 changes: 61 additions & 8 deletions Sprint-3/4-stretch/password-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,64 @@ 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 = "12345cA!";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(true);
});

test("password with less than 5 characters is invalid", () => {
// Arrange
const password = "1Aa%";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password with no uppercase letters is invalid", () => {
// Arrange
const password = "1234ab$";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password with no lowercase letters is invalid", () => {
// Arrange
const password = "1234AB$";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password with no numbers is invalid", () => {
// Arrange
const password = "passWord!";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("password with non-alphanumeric symbols is invalid", () => {
// Arrange
const password = "1234aAv";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});

test("previous passwords in the passwords array are invalid", () => {
// Arrange
const password = "Qwerty1#";
// Act
const result = isValidPassword(password);
// Assert
expect(result).toEqual(false);
});
Loading