-
-
Notifications
You must be signed in to change notification settings - Fork 335
Scotland | ITP JAN-2026 | Tuan Nguyen | Sprint 3 | 1-Implement-and-rewrite-test #1259
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 6 commits
0359cea
1ec84d6
f57a756
39a5b5b
19009a5
eaab882
6649ae9
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,46 @@ | |
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const cardRanks = [ | ||
| "A", | ||
| "2", | ||
| "3", | ||
| "4", | ||
| "5", | ||
| "6", | ||
| "7", | ||
| "8", | ||
| "9", | ||
| "10", | ||
| "J", | ||
| "Q", | ||
| "K", | ||
| ]; | ||
| const cardSuits = ["♠", "♥", "♦", "♣"]; | ||
| if (typeof card !== "string") { | ||
| throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| let rank = card.slice(0, -1); | ||
| let suit = card.slice(-1); | ||
| if ( | ||
| typeof card !== "string" || | ||
|
||
| !cardRanks.includes(rank) || | ||
| !cardSuits.includes(suit) | ||
| ) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| switch (rank) { | ||
| case "A": | ||
| return 11; | ||
| case "J": | ||
| case "Q": | ||
| case "K": | ||
| return 10; | ||
| default: | ||
| return Number(rank); | ||
| } | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -40,13 +80,29 @@ function assertEquals(actualOutput, targetOutput) { | |
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("A♥"), 11); | ||
| assertEquals(getCardValue("J♥"), 10); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| assertEquals(getCardValue("K♣"), 10); | ||
| assertEquals(getCardValue("10♠"), 10); | ||
| assertEquals(getCardValue("5♠"), 5); | ||
|
|
||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("A"); | ||
| console.error("Expected error for 'A' but none was thrown"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("♠"); | ||
| console.error("Expected error for '♠' but none was thrown"); | ||
| } catch (e) {} | ||
|
|
||
| // What other invalid card cases can you think of? | ||
Uh oh!
There was an error while loading. Please reload this page.