-
-
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 6 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,14 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| // TODO: Implement this function | ||
| if (denominator === 0) { | ||
| return false; | ||
| } | ||
| else { | ||
|
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 else statement is technically not required here (given the if statement above would return if the condition was fulfilled, and this is the final case before the function ends) but it's not a breaking change
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. So, is it always possible not to use else? 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. Technically, yes, but it depends on the flow of your program and what you want to achieve. Doing something like this in a function: Will always fall to the final return if the condition is not met. If, however, you had multiple conditions or more code that you wanted/not wanted to execute after the else statement, you could use it as a case that catches all possible branches. |
||
| return Math.abs(numerator) < Math.abs(denominator); | ||
| } | ||
| } | ||
|
|
||
| `` | ||
| // The line below allows us to load the isProperFraction function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = isProperFraction; | ||
|
|
@@ -31,3 +36,13 @@ function assertEquals(actualOutput, targetOutput) { | |
|
|
||
| // Example: 1/2 is a proper fraction | ||
|
Elisabeth-Matulian marked this conversation as resolved.
|
||
| assertEquals(isProperFraction(1, 2), true); | ||
| assertEquals(isProperFraction(7, 1), false); | ||
| assertEquals(isProperFraction(0, 5), true); | ||
| assertEquals(isProperFraction(-1, -2), true); | ||
| assertEquals(isProperFraction(-2, -1), false); | ||
| assertEquals(isProperFraction(-2, 5), true); | ||
| assertEquals(isProperFraction(5, -2), false); | ||
| assertEquals(isProperFraction(5, 5), false); | ||
| assertEquals(isProperFraction(5, 0), false); | ||
| assertEquals(isProperFraction(-2, 0), false); | ||
| `` | ||
|
Elisabeth-Matulian marked this conversation as resolved.
Outdated
|
||
|
Elisabeth-Matulian marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,12 @@ | |
| // 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) | ||
| const cardSuit = card.slice(-1) | ||
| if (cardNum === "A" && "♠♥♦♣".includes(cardSuit)) { 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 +45,34 @@ 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) {} | ||
|
|
||
| try { | ||
| getCardValue("AX"); | ||
|
|
||
| // 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.