-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | 26-ITP-Jan | Miriam Jorna | Sprint 3 | Coursework implement and rewrite tests #1269
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
eddb80b
6fec6d3
bdf116a
05c5304
5f7a512
8f484a4
220aaeb
9c604d3
ab4ad44
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,8 +11,15 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| // TODO: Implement this function | ||
| // denominator cannot be 0 in a proper fraction | ||
| if (denominator === 0) { | ||
|
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. Excellent check for |
||
| return false; | ||
| } | ||
|
|
||
| // The numerator must have a smaller absolute value than the denominator | ||
| 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. | ||
|
|
@@ -31,3 +38,20 @@ function assertEquals(actualOutput, targetOutput) { | |
|
|
||
| // Example: 1/2 is a proper fraction | ||
| assertEquals(isProperFraction(1, 2), true); | ||
| assertEquals(isProperFraction(-1, 2), true); | ||
| assertEquals(isProperFraction(1, -2), true); | ||
| assertEquals(isProperFraction(-1, -2), true); | ||
|
|
||
| // Example: 2/1 is not a proper fraction | ||
| assertEquals(isProperFraction(2, 1), false); | ||
| assertEquals(isProperFraction(-2, 1), false); | ||
| assertEquals(isProperFraction(2, -1), false); | ||
| assertEquals(isProperFraction(-2, -1), false); | ||
|
|
||
| // Example: 0/5 is a proper fraction | ||
| assertEquals(isProperFraction(0, 5), true); | ||
| assertEquals(isProperFraction(0, -5), true); | ||
|
|
||
| // Example: 5/0 is not a proper fraction | ||
| assertEquals(isProperFraction(5, 0), false); | ||
| assertEquals(isProperFraction(-5, 0), false); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,32 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
|
|
||
| if (typeof card !== "string") { | ||
|
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, what you're trying to ensure is that this argument is provided, is a string and can be used down the line. This is a solid approach to do that 👍
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. Thank you for your kind words here and above. Have added a string length criterium. |
||
| throw new Error("Card must be a string"); | ||
| } | ||
|
|
||
| if (card.length < 2) { | ||
| throw new Error("Card must have at least two characteristics"); | ||
| } | ||
|
|
||
| const rank = card.slice(0, -1); | ||
| const suit = card.slice(-1); | ||
|
|
||
| const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
|
|
||
| if (!validRanks.includes(rank) || !validSuits.includes(suit)) { | ||
| throw new Error("Invalid card format"); | ||
| } | ||
|
|
||
| if (rank === "A") { | ||
| return 11; | ||
| } else if (["J", "Q", "K"].includes(rank)) { | ||
| return 10; | ||
| } else { | ||
| return parseInt(rank, 10); | ||
| } | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -39,14 +64,31 @@ function assertEquals(actualOutput, targetOutput) { | |
|
|
||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("A♠"), 11); | ||
| assertEquals(getCardValue("J♠"), 10); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| 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) {} | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| try { | ||
| getCardValue("11♠"); | ||
| console.error("Error was not thrown for invalid rank"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("A♤"); | ||
| console.error("Error was not thrown for invalid suit"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue(123); | ||
| console.error("Error was not thrown for non-string input"); | ||
| } catch (e) {} | ||
|
|
||
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.
This is good work 👍 You're accounting for most types of angles. But there is a problem here. 0 is also sometimes considered a valid angle. Regardless, if your code assumes that it's valid, it should take that into account, and if it thinks 0 degrees is not a valid angle, it should take that into account as well.
How would you do that?
Uh oh!
There was an error while loading. Please reload this page.
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.
Schrödinger's angle... Initially I thought to return an empty string would be best, but on second thought it's better to just name what we have: "Zero angle"