Skip to content
Closed
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
27 changes: 24 additions & 3 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
function getOrdinalNumber(num) {
return "1st";
function getOrdinalNumber(number) {
const lastTwoDigits = number % 100;

// Special cases: 11, 12, 13 always use "th"
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return `${number}th`;
}

const lastDigit = number % 10;

if (lastDigit === 1) {
return `${number}st`;
}

if (lastDigit === 2) {
return `${number}nd`;
}

if (lastDigit === 3) {
return `${number}rd`;
}

return `${number}th`;
}

module.exports = getOrdinalNumber;
module.exports = getOrdinalNumber;
38 changes: 38 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,41 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending with 2 (but not 12)
// When the number ends with 2, except those ending with 12,
// Then the function should return a string by appending "nd" to the number.
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

// Case 3: Numbers ending with 3 (but not 13)
// When the number ends with 3, except those ending with 13,
// Then the function should return a string by appending "rd" to the number.
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});

// Case 4: Numbers ending with 11, 12, 13 (special cases)
// These numbers should always end with "th".
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

// Case 5: All other numbers
// Numbers that do not end with 1, 2, or 3 should end with "th".
test("should append 'th' for all other numbers", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(24)).toEqual("24th");
expect(getOrdinalNumber(100)).toEqual("100th");
});
24 changes: 24 additions & 0 deletions Sprint-3/4-stretch/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function validateCreditCard(number) {
// Ensure number is 16 digits and only contains digits
if (number.length !== 16 || !/^\d+$/.test(number)) return false;

// Ensure at least 2 different digits are present
const uniqueDigits = new Set(number);
if (uniqueDigits.size < 2) return false;

// Ensure the last digit is even
const lastDigit = parseInt(number[number.length - 1], 10);
if (lastDigit % 2 !== 0) return false;

// Ensure the sum of digits is greater than 16
const sum = number.split('').reduce((acc, digit) => acc + parseInt(digit, 10), 0);
if (sum <= 16) return false;

// If all checks pass, the number is valid
return true;
}
// Function to validate a credit card number according to the rules in card-validator.md

console.log(validateCreditCard('9999777788880000')); // true
console.log(validateCreditCard('4444444444444444')); // false
console.log(validateCreditCard('6666666666666661')); // false
Loading