-
-
Notifications
You must be signed in to change notification settings - Fork 241
Manchester | 25-ITP-May | Fithi Teklom | Sprint 3 | 3-stretch #868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 14 commits
31f50ad
a61f9b1
2a422f5
04f5a7f
7e9160d
6c17f89
94de70c
ec34846
243fa1c
bc06097
7da75fd
4d31f44
cf52d08
3889214
f1b775f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,61 @@ | ||
|
|
||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| const remainder10 = num % 10; | ||
| const remainder100 = num % 100; | ||
|
|
||
| if (remainder100 >= 11 && remainder100 <= 13) { | ||
| return `${num}th`; | ||
| } | ||
|
|
||
| switch (remainder10) { | ||
| case 1: | ||
| return `${num}st`; | ||
| case 2: | ||
| return `${num}nd`; | ||
| case 3: | ||
| return `${num}rd`; | ||
| default: | ||
| return `${num}th`; | ||
| } | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
|
|
||
|
|
||
| const getOrdinalNumber = require("./getOrdinalNumber"); | ||
|
|
||
| describe("getOrdinalNumber()", () => { | ||
| test("should return '1st' for 1", () => { | ||
| expect(getOrdinalNumber(1)).toBe("1st"); | ||
| }); | ||
|
|
||
| test("should return '2nd' for 2", () => { | ||
| expect(getOrdinalNumber(2)).toBe("2nd"); | ||
| }); | ||
|
|
||
| test("should return '3rd' for 3", () => { | ||
| expect(getOrdinalNumber(3)).toBe("3rd"); | ||
| }); | ||
|
|
||
| test("should return '4th' for 4", () => { | ||
| expect(getOrdinalNumber(4)).toBe("4th"); | ||
| }); | ||
|
|
||
| test("should return '11th', '12th', '13th' for special cases", () => { | ||
| expect(getOrdinalNumber(11)).toBe("11th"); | ||
| expect(getOrdinalNumber(12)).toBe("12th"); | ||
| expect(getOrdinalNumber(13)).toBe("13th"); | ||
| }); | ||
|
|
||
| test("should return correct suffixes for 21, 22, 23", () => { | ||
| expect(getOrdinalNumber(21)).toBe("21st"); | ||
| expect(getOrdinalNumber(22)).toBe("22nd"); | ||
| expect(getOrdinalNumber(23)).toBe("23rd"); | ||
| }); | ||
|
|
||
| test("should return '111th' for numbers ending with 11, 12, 13 even if larger", () => { | ||
| expect(getOrdinalNumber(111)).toBe("111th"); | ||
| expect(getOrdinalNumber(112)).toBe("112th"); | ||
| expect(getOrdinalNumber(113)).toBe("113th"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const repeat = require("./repeat"); | ||
|
|
||
| function repeat() { | ||
| return "hellohellohello"; | ||
| } | ||
|
|
||
| module.exports = repeat; | ||
|
|
||
| test("should repeat the given word three times", () => { | ||
| expect(repeat("hi", 3)).toBe("hihihi"); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| function validateCreditCard(number) { | ||
| const numStr = String(number); | ||
|
|
||
| // Must be exactly 16 digits, only numbers | ||
| if (!/^\d{16}$/.test(numStr)) return false; | ||
|
|
||
| // Must contain at least two different digits | ||
| const uniqueDigits = new Set(numStr); | ||
| if (uniqueDigits.size < 2) return false; | ||
|
|
||
| // Last digit must be even | ||
| const lastDigit = Number(numStr[numStr.length - 1]); | ||
| if (lastDigit % 2 !== 0) return false; | ||
|
|
||
| // Sum must be greater than 16 | ||
| const sum = numStr | ||
| .split("") | ||
| .map(Number) | ||
| .reduce((a, b) => a + b, 0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the function still work if line 19 is changed to
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function will not work correctly in all cases if you remove the initial value.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was asking, will your function work differently if line 19 is changed from Can you find an example where your function would work differently? Suggestion, look up how |
||
| if (sum <= 16) return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| module.exports = validateCreditCard; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * Validates a credit card number based on specific rules: | ||
| * - Must be 16 digits | ||
| * - Must have at least two different digits | ||
| * - Final digit must be even | ||
| * - Sum of all digits must be greater than 16 | ||
| * | ||
| * @param {string} cardNumber - The credit card number to validate | ||
| * @returns {boolean} - true if valid, false if invalid | ||
| */ | ||
| function validateCreditCard(cardNumber) { | ||
| // Rule 1: Check if length is exactly 16 digits and all are numbers | ||
| if (!/^\d{16}$/.test(cardNumber)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Convert string to array of numbers | ||
| const digits = cardNumber.split('').map(Number); | ||
|
|
||
| // Rule 2: Check for at least two different digits | ||
| const uniqueDigits = new Set(digits); | ||
| if (uniqueDigits.size < 2) { | ||
| return false; | ||
| } | ||
|
|
||
| // Rule 3: Check if the final digit is even | ||
| if (digits[digits.length - 1] % 2 !== 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // Rule 4: Check if sum of all digits is greater than 16 | ||
| const sum = digits.reduce((acc, val) => acc + val, 0); | ||
| if (sum <= 16) { | ||
| return false; | ||
| } | ||
|
|
||
| // All rules passed | ||
| return true; | ||
| } | ||
|
|
||
| // Example usage: | ||
| console.log(validateCreditCard("9999777788880000")); // true | ||
| console.log(validateCreditCard("6666666666661666")); // true | ||
| console.log(validateCreditCard("a92332119c011112")); // false | ||
| console.log(validateCreditCard("4444444444444444")); // false | ||
| console.log(validateCreditCard("1111111111111110")); // false | ||
| console.log(validateCreditCard("6666666666666661")); // false | ||
|
|
||
| module.exports = validateCreditCard; | ||
|
|
||
| const validateCreditCard = require('./credit-card-validator'); | ||
|
|
||
| test("valid credit card number", () => { | ||
| expect(validateCreditCard("9999777788880000")).toBe(true); | ||
| }); | ||
|
|
||
| test("invalid credit card number (all same digits)", () => { | ||
| expect(validateCreditCard("4444444444444444")).toBe(false); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const validateCreditCard = require("./validateCreditCard"); | ||
|
|
||
| test("valid credit card numbers should return true", () => { | ||
| expect(validateCreditCard("9999777788880000")).toBe(true); | ||
| expect(validateCreditCard("6666666666661666")).toBe(true); | ||
| }); | ||
|
|
||
| test("invalid cards with letters should return false", () => { | ||
| expect(validateCreditCard("a92332119c011112")).toBe(false); | ||
| }); | ||
|
|
||
| test("invalid cards with all same digits should return false", () => { | ||
| expect(validateCreditCard("4444444444444444")).toBe(false); | ||
| }); | ||
|
|
||
| test("invalid cards with sum less than 16 should return false", () => { | ||
| expect(validateCreditCard("1111111111111110")).toBe(false); | ||
| }); | ||
|
|
||
| test("invalid cards with odd final digit should return false", () => { | ||
| expect(validateCreditCard("6666666666666661")).toBe(false); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.