-
-
Notifications
You must be signed in to change notification settings - Fork 338
London | 26-ITP-Jan | Boualem Larbi Djebbour | sprint 3 | implement and rewrite tests coursework #1261
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?
London | 26-ITP-Jan | Boualem Larbi Djebbour | sprint 3 | implement and rewrite tests coursework #1261
Changes from 9 commits
0c4631a
4a2804d
6b4ee50
7d502dc
82958b5
c286e4a
a70220f
bc11ec7
7eab738
3f7c3f8
6769341
f231516
608f1db
9a3c314
c099074
fa537b2
36e8c14
af851f3
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,17 @@ | |
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| if ( | ||
| card.length < 2 || | ||
| card.length > 3 || | ||
| !["♠", "♥", "♦", "♣"].includes(card.slice(-1)) | ||
| ) | ||
| throw new Error("invalid suit"); | ||
| if (card[0] === "A") return 11; | ||
| if (["J", "Q", "K"].includes(card[0])) return 10; | ||
| if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(card[0])) | ||
| return card[0]; // the parseint() or Number() can be used to convert the string to a number | ||
| throw new Error("invalid rank"); | ||
|
||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -41,6 +52,11 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| assertEquals(getCardValue("A♥"), 11); | ||
| assertEquals(getCardValue("J♣"), 10); | ||
| assertEquals(getCardValue("Q♠"), 10); | ||
| assertEquals(getCardValue("2♦"), 2); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
|
|
@@ -50,3 +66,37 @@ try { | |
| } catch (e) {} | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| try { | ||
| getCardValue("S"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("AJKP"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("A❦"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("29"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("♦,♣"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("2345"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue(""); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,13 +8,87 @@ const getCardValue = require("../implement/3-get-card-value"); | |
| test(`Should return 11 when given an ace card`, () => { | ||
| expect(getCardValue("A♠")).toEqual(11); | ||
| }); | ||
| test(`Should return 11 when given an ace card`, () => { | ||
| expect(getCardValue("A♥")).toEqual(11); | ||
| }); | ||
| test(`Should return 11 when given an ace card`, () => { | ||
| expect(getCardValue("A♦")).toEqual(11); | ||
| }); | ||
| test(`Should return 11 when given an ace card`, () => { | ||
| expect(getCardValue("A♣")).toEqual(11); | ||
| }); | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Suggestion: Group the remaining test data into these categories: | ||
| // Number Cards (2-10) | ||
| test(`Should return 2 when given a 2 card`, () => { | ||
| expect(getCardValue("2♠")).toEqual(2); | ||
| }); | ||
| test(`Should return 3 when given a 3 card`, () => { | ||
| expect(getCardValue("3♥")).toEqual(3); | ||
| }); | ||
| test(`Should return 4 when given a 4 card`, () => { | ||
| expect(getCardValue("4♦")).toEqual(4); | ||
| }); | ||
| test(`Should return 5 when given a 5 card`, () => { | ||
| expect(getCardValue("5♣")).toEqual(5); | ||
| }); | ||
| test(`Should return 6 when given a 6 card`, () => { | ||
| expect(getCardValue("6♥")).toEqual(6); | ||
| }); | ||
| test(`Should return 7 when given a 7 card`, () => { | ||
| expect(getCardValue("7♠")).toEqual(7); | ||
| }); | ||
| test(`Should return 8 when given a 8 card`, () => { | ||
| expect(getCardValue("8♦")).toEqual(8); | ||
| }); | ||
| test(`Should return 9 when given a 9 card`, () => { | ||
| expect(getCardValue("9♠")).toEqual(9); | ||
| }); | ||
| test(`Should return 10 when given a 10 card`, () => { | ||
| expect(getCardValue("10♣")).toEqual(10); | ||
| }); | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Comment on lines
+17
to
+27
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 think your function can pass this test yet.
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 changed the returned outcome to a number in the function |
||
|
|
||
| // Face Cards (J, Q, K) | ||
| test(`Should return 10 when given a J card`, () => { | ||
| expect(getCardValue("J♠")).toEqual(10); | ||
| }); | ||
| test(`Should return 10 when given a Q card`, () => { | ||
| expect(getCardValue("Q♦")).toEqual(10); | ||
| }); | ||
| test(`Should return 10 when given a K card`, () => { | ||
| expect(getCardValue("K♥")).toEqual(10); | ||
| }); | ||
| // Invalid Cards | ||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("1♠")).toThrow(); | ||
| }); | ||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("2❦")).toThrow(); | ||
| }); | ||
|
|
||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("11♥")).toThrow(); | ||
| }); | ||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("B♦")).toThrow(); | ||
| }); | ||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("Z♣")).toThrow(); | ||
| }); | ||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("♣2")).toThrow(); | ||
| }); | ||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("s")).toThrow(); | ||
| }); | ||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("♣♥♠♦")).toThrow(); | ||
| }); | ||
|
|
||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("")).toThrow(); | ||
| }); | ||
|
|
||
| // 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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.