-
-
Notifications
You must be signed in to change notification settings - Fork 336
London|ITP-Jan-2026|Ping Wang|Sprint 3|1-implement-and -rewrite -tests #1103
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
3441cd5
9fd74bc
2940579
9542809
c43a321
43a9f32
630ae85
8850c1c
2f4e882
eac9e9a
e6ce67c
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,8 +22,23 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| } | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
| const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; | ||
|
|
||
| const suit = card.slice(-1); | ||
| const rank = card.slice(0, -1); | ||
|
|
||
|
|
||
| if (!validSuits.includes(suit) || !validRanks.includes(rank)) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| if (rank === "A") return 11; | ||
| if (["J", "Q", "K"].includes(rank)) return 10; | ||
|
|
||
| return Number(rank); | ||
|
Comment on lines
+32
to
+39
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. Nice, I really like the use of the "guard pattern" here to deal with errors first 👍🏻. |
||
| } | ||
|
|
||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
|
|
@@ -38,15 +53,45 @@ function assertEquals(actualOutput, targetOutput) { | |
| } | ||
|
|
||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| // Examples 1 numbers: | ||
| // Examples 1 numbers: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("5♣"), 5); | ||
|
|
||
| //Examples A: | ||
| assertEquals(getCardValue("A♦"), 11) | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| //Examples J Q K:, | ||
|
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. Seems like we might be missing a case here? |
||
| assertEquals(getCardValue("Q♣"), 10) | ||
| assertEquals(getCardValue("K♠"), 10) | ||
| assertEquals(getCardValue("J♣"), 10) | ||
|
|
||
| //Examples invalid numbers: | ||
| assertThrow(()=>getCardValue("1♥")) | ||
| assertThrow(()=>getCardValue("11♥")) | ||
|
|
||
| //Examples invalid suit: | ||
| assertThrow(()=>getCardValue("A*")) | ||
|
|
||
| //Examples missing rank or suit: | ||
| assertThrow(()=>getCardValue("♥")) | ||
| assertThrow(()=>getCardValue("5")) | ||
|
|
||
| //Example extra suit: | ||
| assertThrow(()=>getCardValue("Q♠♠")) | ||
|
|
||
|
|
||
| function assertThrow(fn){ | ||
| try { fn() | ||
| // we try to run this function, if it throws, stop running this bit and run the catch below | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
| } catch (error) { | ||
| // if the above code throws, we catch the error here, that stops the whole program crashing | ||
| console.log('there was an error getting card value!',error) | ||
| }} | ||
|
|
||
| console.log(' we finished running getCardValue') | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,39 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); | |
|
|
||
| // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. | ||
|
|
||
| // Special case: numerator is zero | ||
| /// Special case: denominator is zero | ||
| test(`should return false when denominator is zero`, () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| //case:absolute numerator is bigger than absolute denominator | ||
| test ('should return false when numerator absolute is bigger than denominator absolute',() => { | ||
|
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 these all possible combinations? Keep in mind that you can group similar tests together with Jest. |
||
| expect(isProperFraction(5, -4)).toEqual(false); | ||
| expect(isProperFraction(-5, 4)).toEqual(false); | ||
| expect(isProperFraction(-5, -4)).toEqual(false); | ||
| expect(isProperFraction(5, 4)).toEqual(false); | ||
| }); | ||
|
|
||
| //case:numerator is the same as denominator | ||
| test('should return false if numerator is the same as denominator', () =>{ | ||
| expect(isProperFraction(-9,-9)).toEqual(false) | ||
| expect(isProperFraction(4,4)).toEqual(false) | ||
| }) | ||
|
|
||
| //case: absolute numerator is less than absolute denominator | ||
| test ('should return true when numerator absolute is less than denominator absolute',() => { | ||
| expect(isProperFraction(3, 8)).toEqual(true); | ||
| expect(isProperFraction(-3, 8)).toEqual(true); | ||
| expect(isProperFraction(3, -8)).toEqual(true); | ||
| expect(isProperFraction(-3, -8)).toEqual(true); | ||
| }); | ||
|
|
||
|
|
||
| //case: numerator is zero | ||
| test(`should return true when nominator is zero`, () => { | ||
| expect(isProperFraction(0,-3)).toEqual(true); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
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.
Can you think of a couple more edge cases that could happen here? Try to think of boundaries and possible invalid values.