-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | JAN-2026 ITP | Asha Ahmed | Sprint 3 | implement and rewrite tests coursework #1035
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
b150432
bec514f
72a8a7d
5fa7695
7e4cb27
56c990b
ad8efb8
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,14 @@ | |
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| if (!["♠", "♣", "♦", "♥"].includes(card.slice(-1))) | ||
| throw new Error("Invalid card rank."); | ||
| const rank = card.slice(0, card.length - 1); | ||
| if (rank === "A") return 11; | ||
| if (["10", "J", "Q", "K"].includes(rank)) return 10; | ||
| if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank)) | ||
| return Number(rank); | ||
| throw new Error("Invalid card rank."); | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -41,6 +49,12 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| assertEquals(getCardValue("6♥"), 6); | ||
|
|
||
| assertEquals(getCardValue("J♣"), 10); | ||
|
|
||
| assertEquals(getCardValue("A♦"), 11); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
|
|
@@ -50,3 +64,33 @@ try { | |
| } catch (e) {} | ||
|
|
||
| // What other invalid card cases can you think of? | ||
|
|
||
| try { | ||
|
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. An empty catch block is not good practice. Fix the catch blocks in this file
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. My answers are based on the example given above. 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. Avoid empty catch. At the very least, there should be a comment in the empty block explaining why you're swallowing the exception at that point or output the exception. Look up the standard on how to write a try and catch block; it would help fix the syntax.
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. I'm still learning JavaScript, so I’ll look into catch blocks as I’m not entirely sure what you mean yet. My understanding was that the code is technically correct, and my answers were based on the example provided by CodeYourFuture above. Given that, it feels like a big change to fix the file itself rather than respond to the questions asked. |
||
| getCardValue("9K"); | ||
|
|
||
| console.log("Error was not thrown for invalid card"); | ||
| } catch (error) {} | ||
|
|
||
| try { | ||
| getCardValue(""); | ||
|
|
||
| console.log("Error was not thrown for invalid card"); | ||
| } catch (error) {} | ||
|
|
||
| try { | ||
| getCardValue("ABC"); | ||
|
|
||
| console.log("Error was not thrown for invalid card"); | ||
| } catch (error) {} | ||
|
|
||
| try { | ||
| getCardValue("A"); | ||
|
|
||
| console.log("Error was not thrown for invalid card"); | ||
| } catch (error) {} | ||
|
|
||
| try { | ||
| getCardValue("JK"); | ||
|
|
||
| console.log("Error was not thrown for invalid card"); | ||
| } catch (error) {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,74 @@ const getCardValue = require("../implement/3-get-card-value"); | |
| // Case 1: Ace (A) | ||
| test(`Should return 11 when given an ace card`, () => { | ||
| expect(getCardValue("A♠")).toEqual(11); | ||
| expect(getCardValue("A♣")).toEqual(11); | ||
|
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. These are duplicated test lines. Is there a reason for this?
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. I don't think this is a duplication. We have two codes who have different purpose: one for spades and one for clubs. Can you please check again? |
||
| expect(getCardValue("A♦")).toEqual(11); | ||
| expect(getCardValue("A♥")).toEqual(11); | ||
| }); | ||
|
|
||
| // Case 2: Handle Number Cards (2-10): | ||
| test("should return the appropriate number from 2 to 10", () => { | ||
| expect(getCardValue("2♥")).toEqual(2); | ||
| expect(getCardValue("3♥")).toEqual(3); | ||
| expect(getCardValue("4♥")).toEqual(4); | ||
| expect(getCardValue("5♥")).toEqual(5); | ||
| expect(getCardValue("6♥")).toEqual(6); | ||
| expect(getCardValue("7♥")).toEqual(7); | ||
| expect(getCardValue("8♥")).toEqual(8); | ||
| expect(getCardValue("9♥")).toEqual(9); | ||
| expect(getCardValue("10♥")).toEqual(10); | ||
| expect(getCardValue("2♠")).toEqual(2); | ||
| expect(getCardValue("3♠")).toEqual(3); | ||
| expect(getCardValue("4♠")).toEqual(4); | ||
| expect(getCardValue("5♠")).toEqual(5); | ||
| expect(getCardValue("6♠")).toEqual(6); | ||
| expect(getCardValue("7♠")).toEqual(7); | ||
| expect(getCardValue("8♠")).toEqual(8); | ||
| expect(getCardValue("9♠")).toEqual(9); | ||
| expect(getCardValue("10♠")).toEqual(10); | ||
| expect(getCardValue("2♦")).toEqual(2); | ||
| expect(getCardValue("3♦")).toEqual(3); | ||
| expect(getCardValue("4♦")).toEqual(4); | ||
| expect(getCardValue("5♦")).toEqual(5); | ||
| expect(getCardValue("6♦")).toEqual(6); | ||
| expect(getCardValue("7♦")).toEqual(7); | ||
| expect(getCardValue("8♦")).toEqual(8); | ||
| expect(getCardValue("9♦")).toEqual(9); | ||
| expect(getCardValue("10♦")).toEqual(10); | ||
| expect(getCardValue("2♣")).toEqual(2); | ||
| expect(getCardValue("3♣")).toEqual(3); | ||
| expect(getCardValue("4♣")).toEqual(4); | ||
| expect(getCardValue("5♣")).toEqual(5); | ||
| expect(getCardValue("6♣")).toEqual(6); | ||
| expect(getCardValue("7♣")).toEqual(7); | ||
| expect(getCardValue("8♣")).toEqual(8); | ||
| expect(getCardValue("9♣")).toEqual(9); | ||
| expect(getCardValue("10♣")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 3: Handle 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); | ||
| expect(getCardValue("J♦")).toEqual(10); | ||
| expect(getCardValue("Q♦")).toEqual(10); | ||
| expect(getCardValue("K♦")).toEqual(10); | ||
| expect(getCardValue("J♣")).toEqual(10); | ||
| expect(getCardValue("Q♣")).toEqual(10); | ||
| expect(getCardValue("K♣")).toEqual(10); | ||
| expect(getCardValue("J♥")).toEqual(10); | ||
| expect(getCardValue("Q♥")).toEqual(10); | ||
| expect(getCardValue("K♥")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 4: Handle Invalid Cards: | ||
| test("Should return 'Invalid card rank.' for invalid cards", () => { | ||
| expect(() => getCardValue("KJ")).toThrow("Invalid card rank."); | ||
| expect(() => getCardValue("AK")).toThrow("Invalid card rank."); | ||
| expect(() => getCardValue(" ")).toThrow("Invalid card rank."); | ||
| expect(() => getCardValue("S♠")).toThrow("Invalid card rank."); | ||
| expect(() => getCardValue("J♠♠")).toThrow("Invalid card rank."); | ||
| }); | ||
|
|
||
| // Suggestion: Group the remaining test data into these categories: | ||
|
|
@@ -17,4 +85,3 @@ test(`Should return 11 when given an ace card`, () => { | |
| // 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.
The catch block can be improved. it's currently empty
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.
You are referring to the original CodeYourFuture code example, which I did not write. The question I was asked was “What other invalid card cases can you think of?” (see below), not to modify the code you are commenting on.