-
-
Notifications
You must be signed in to change notification settings - Fork 307
London | 26-ITP-Jan | Abdul Moiz | Sprint 3 | implement-and-rewrite #915
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 all commits
5f845d0
2b5822f
a08e5c7
05ac5d6
b7ff208
2fdc1ca
d6ce110
1ba0eff
7d4b528
ac777a7
03452aa
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 |
|---|---|---|
|
|
@@ -22,7 +22,29 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const rank = card.slice(0, -1); | ||
| const lastChar = card.slice(-1); | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
|
|
||
| if (!validSuits.includes(lastChar)) { | ||
| throw new Error("Invalid card suit"); | ||
| } | ||
|
|
||
| if (rank === "A") return 11; | ||
| if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") return 10; | ||
|
|
||
| const rankNum = Number(rank); | ||
| if ( | ||
| !Number.isNaN(rankNum) && | ||
| Number.isInteger(rankNum) && | ||
| rankNum >= 2 && | ||
| rankNum < 10 && | ||
| rank === rankNum.toString() | ||
| ) { | ||
| return rankNum; | ||
| } | ||
|
Comment on lines
36
to
45
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 don't see any statement involving Currently, a card value like ""0x02♠" or "3.1416♠" can still pass the check on line 37.
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. Apologies, I got it mixed up with a different task. It is resolved now. |
||
|
|
||
| throw new Error("Invalid card rank"); | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -41,12 +63,34 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| // Handle Number Cards (2-10): | ||
| // Given a card with a rank between "2" and "9", | ||
| // When the function is called with such a card, | ||
| // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| assertEquals(fiveofHearts, 5); | ||
|
|
||
| // Handle Face Cards (J, Q, K): | ||
| // Given a card with a rank of "10," "J," "Q," or "K", | ||
| // When the function is called with such a card, | ||
| // Then it should return the value 10, as these cards are worth 10 points each in blackjack. | ||
| const kingofDiamonds = getCardValue("K♦"); | ||
| assertEquals(kingofDiamonds, 10); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
| // Handle Ace (A): | ||
| // Given a card with a rank of "A", | ||
| // When the function is called with an Ace, | ||
| // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. | ||
| const aceofClubs = getCardValue("A♣"); | ||
| assertEquals(aceofClubs, 11); | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| // Handle Invalid Cards: | ||
| // Given a card with an invalid rank (neither a number nor a recognized face card), | ||
| // When the function is called with such a card, | ||
| // Then it should throw an error indicating "Invalid card rank." | ||
| try { | ||
| getCardValue("1♠"); | ||
| } catch (error) { | ||
| assertEquals(error.message, "Invalid card rank"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,31 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); | |
| test(`should return false when denominator is zero`, () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return true for proper fraction with positive numerator and denominator`, () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| }); | ||
|
|
||
| test(`should return false for improper fraction with positive numerator and denominator`, () => { | ||
| expect(isProperFraction(3, 2)).toEqual(false); | ||
| }); | ||
|
|
||
|
Comment on lines
+12
to
+19
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. What about negative numbers and different combinations of positive/negative/zero? Think also what edge case to check?
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. Added extra tests for negative numerators/denominators and all are passing. |
||
| test(`should return true for numerator zero and positive denominator`, () => { | ||
| expect(isProperFraction(0, 5)).toEqual(true); | ||
| }); | ||
|
|
||
| test(`should return false for denominator zero`, () => { | ||
| expect(isProperFraction(5, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return false for negative denominator`, () => { | ||
| expect(isProperFraction(5, -2)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return false for negative numerator`, () => { | ||
| expect(isProperFraction(-5, 2)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return true for negative numerator and negative denominator where numerator is smaller.`, () => { | ||
| expect(isProperFraction(-1, -2)).toEqual(true); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,36 @@ test(`Should return 11 when given an ace card`, () => { | |
| // Face Cards (J, Q, K) | ||
| // Invalid Cards | ||
|
|
||
| // Case 2: Number Cards (2-10) | ||
| test(`Should return the correct value for number cards`, () => { | ||
| expect(getCardValue("2♣")).toEqual(2); | ||
| expect(getCardValue("5♦")).toEqual(5); | ||
| expect(getCardValue("10♥")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 3: Face Cards (J, Q, K) | ||
| test(`Should return 10 for face cards`, () => { | ||
| expect(getCardValue("J♠")).toEqual(10); | ||
| expect(getCardValue("Q♦")).toEqual(10); | ||
| expect(getCardValue("K♥")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 4: Invalid Cards | ||
| test(`Should throw an error for Invalid card rank for invalid cards`, () => { | ||
| expect(() => getCardValue("1♠")).toThrow("Invalid card rank"); | ||
| expect(() => getCardValue("11♥")).toThrow("Invalid card rank"); | ||
| expect(() => getCardValue("Z♦")).toThrow("Invalid card rank"); | ||
| expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank"); | ||
| expect(() => getCardValue("3.1416♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| // Case 5: Invalid suit character | ||
| test(`Should throw an error for invalid suit character`, () => { | ||
| expect(() => getCardValue("1*")).toThrow("Invalid card suit"); | ||
| expect(() => getCardValue("11-")).toThrow("Invalid card suit"); | ||
| expect(() => getCardValue("Z/")).toThrow("Invalid card suit"); | ||
| }); | ||
|
|
||
|
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.
|
||
| // To learn how to test whether a function throws an error as expected in Jest, | ||
| // please refer to the Jest documentation: | ||
| // https://jestjs.io/docs/expect#tothrowerror | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but an approach similar to that at line 29 is probably cleaner.