-
-
Notifications
You must be signed in to change notification settings - Fork 395
London | 26-ITP-May | Russom Gebremeskel | Sprint 3 | 1 Implement and Rewrite Tests #1529
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
c4261fd
3331c94
d16b7fb
c3617ce
29fc3b3
be2fbc2
e05bc8f
6794148
7c99da6
196cd5f
f4ea51e
5fb7cfb
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,41 @@ | |
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| if (card <= 1 && card >= 10 || typeof card !== "string") { | ||
| // Here the type of values are checked whether they are string. | ||
| return card + " Invalid card"; | ||
| } | ||
|
|
||
| const validRanks = [ // An array of ranks are created here | ||
| "A", | ||
| "2", | ||
| "3", | ||
| "4", | ||
| "5", | ||
| "6", | ||
| "7", | ||
| "8", | ||
| "9", | ||
| "10", | ||
| "J", | ||
| "Q", | ||
| "K", | ||
| ]; | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; // An array of suits are created here. | ||
| const suit = card.slice(-1); | ||
| const rank = card.slice(0, -1); | ||
|
|
||
| if (!validRanks.includes(rank) || !validSuits.includes(suit)) { //Here the values that are stored in rank and suit are checked whether they are valid are inside the validRank and validSuits arrays. | ||
| return card + " Invalid card"; | ||
|
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. Its better to throw an error here rather than returning the |
||
| } | ||
|
|
||
| if (rank === "A") { // Here the code is testing wether the rank is an Ace. | ||
| return 11; | ||
| } else if (rank === "J" || rank === "Q" || rank === "K") { // Here the ranks are checked if they are J, Q or K and it returns 10 | ||
| return 10; | ||
| } else { // Lastly any rank between 2 and 10 get checked and the same number of value returned. | ||
| return Number(rank); | ||
| } | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,34 @@ test(`Should return 11 when given an ace card`, () => { | |
| expect(getCardValue("A♠")).toEqual(11); | ||
| }); | ||
|
|
||
| // Case 2: Face Cards (J, Q, K) | ||
| test(`Should return 10 when given a face card`, () => { | ||
| expect(getCardValue("K♦")).toEqual(10); | ||
| expect(getCardValue("Q♥")).toEqual(10); | ||
| expect(getCardValue("J♣")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 3: Number Cards (2-10) | ||
| test(`Should return the card value when given number card`, () => { | ||
| 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 4: Invalid Cards | ||
| test(`Should return invalid cards when given a face card`, () => { | ||
|
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. please review the test cases and make sure they are passing. |
||
| expect(getCardValue("1♦")).toEqual("Invalid card"); | ||
| expect(getCardValue("10")).toEqual("Invalid card"); | ||
| expect(getCardValue("AZ")).toEqual("Invalid card"); | ||
| expect(getCardValue("")).toEqual("Invalid card"); | ||
| }); | ||
|
|
||
| // Suggestion: Group the remaining test data into these categories: | ||
| // Number Cards (2-10) | ||
| // Face Cards (J, Q, K) | ||
|
|
@@ -17,4 +45,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.
please review the condition
card <= 1 && card >= 10.