-
-
Notifications
You must be signed in to change notification settings - Fork 337
Glasgow | Jan-26 | Elisabeth Matulian | Sprint 3 | Coursework 3 implement and rewrite #1016
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 5 commits
4c160fb
fb170d6
ca7ea04
dfc3ad2
c47b556
6874b2e
9e2dc09
7cd95ff
22cc737
6bff13c
ef9ea5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Elisabeth-Matulian marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,11 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
|
Elisabeth-Matulian marked this conversation as resolved.
|
||
| // TODO: Implement this function | ||
| const cardNum = card.slice(0, -1) | ||
| if (cardNum === "A") { return 11} | ||
| else if (["J", "Q", "K"].includes(cardNum)) { return 10} | ||
| else if (Number(cardNum) >= 2 && Number(cardNum) <= 10) {return Number(cardNum)} | ||
| else throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -40,13 +44,27 @@ 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("2♥"), 2); | ||
| assertEquals(getCardValue("10♥"), 10); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| // 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) {} | ||
|
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. Let's make the catch blocks a bit more interesting. How can we assert the error content against what we would expect the function to throw?
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 need to somehow implement the assertEquals function in try catch, but I haven't found any examples. 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. A little hint, what would happen if you temporarily add a 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. Also, for javascript I highly recommend checking MDN for documentation and references. You might find this one useful to find your answer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#examples
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. This took a while for me but was pretty helpful. Thanks. Looks like it works |
||
| try { | ||
| getCardValue("♦Q"); | ||
|
|
||
| // 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("11♦"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
| // What other invalid card cases can you think of? | ||
|
Elisabeth-Matulian marked this conversation as resolved.
|
|
Elisabeth-Matulian marked this conversation as resolved.
|
Uh oh!
There was an error while loading. Please reload this page.