-
-
Notifications
You must be signed in to change notification settings - Fork 389
Cape Town | 25-ITP-May | Asanda Dunn | Sprint 3 | Coursework/sprint 3 implement and rewrite #1453
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
9a8f076
25fc120
3afdd04
a69d461
41df520
610f195
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 |
|---|---|---|
|
|
@@ -23,6 +23,32 @@ | |
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| if (typeof card !== "string" || card.length < 2) { | ||
| throw new Error("Invalid card format"); | ||
| } | ||
| const rank = card.slice(0, -1); | ||
| const suit = card.slice(-1); | ||
|
|
||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
| if (!validSuits.includes(suit)) { | ||
| throw new Error("Invalid suit"); | ||
| } | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
| if (["J", "Q", "K"].includes(rank)) { | ||
| return 10; | ||
| } | ||
| const numericValue = parseInt(rank, 10); | ||
| if ( | ||
| !isNaN(numericValue) && | ||
| numericValue >= 2 && | ||
| numericValue <= 10 && | ||
| String(numericValue) === rank | ||
|
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 does this last condition do? |
||
| ) { | ||
| return numericValue; | ||
| } | ||
| throw new Error("Invalid rank"); | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -52,3 +78,18 @@ try { | |
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("2♥"), 2); | ||
| assertEquals(getCardValue("10♥"), 10); | ||
| assertEquals(getCardValue("A♣"), 11); | ||
| assertEquals(getCardValue("J♦"), 10); | ||
| assertEquals(getCardValue("Q♠"), 10); | ||
| assertEquals(getCardValue("K♣"), 10); | ||
|
|
||
| assertThrows("invalid"); | ||
|
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. Did you try running this, does "assertThrows" work here? |
||
| assertThrows("A"); | ||
| assertThrows("♠"); | ||
| assertThrows("1♥"); | ||
| assertThrows("Z♠"); | ||
| assertThrows("10"); | ||
| assertThrows("A♥♠"); | ||
| 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); | ||
| }); | ||
|
|
||
| // Case 1: Standard Positive Numbers | ||
| test("should handle standard positive fractions correctly", () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(5, 3)).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 2: Equal Numbers (Boundary) | ||
| test("should return false when numerator and denominator are equal", () => { | ||
| expect(isProperFraction(5, 5)).toEqual(false); // Equals 1 whole (Improper) | ||
| }); | ||
|
|
||
| // Case 3: Numerator is Zero | ||
| test("should return true when numerator is zero and denominator is positive", () => { | ||
| expect(isProperFraction(0, 5)).toEqual(true); | ||
| }); | ||
|
|
||
| // Case 4: Negative Numbers | ||
| test("should handle negative integers according to mathematical comparison rules", () => { | ||
| expect(isProperFraction(-5, -2)).toEqual(true); | ||
|
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. Are you sure these tests are correct for valid proper fractions? |
||
| expect(isProperFraction(-1, -4)).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 5: Mixed Positive and Negative Numbers | ||
| test("should return true when only the numerator is negative", () => { | ||
| expect(isProperFraction(-1, 2)).toEqual(true); | ||
| expect(isProperFraction(1, -2)).toEqual(false); | ||
| }); | ||
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.
Does your testing consider valid proper fractions that include negatives?