-
-
Notifications
You must be signed in to change notification settings - Fork 336
London | 26-ITP-Jan | Divine Mankrado | Sprint 3 | Implement and Rewrite Tests #1196
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
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 |
|---|---|---|
| @@ -1,33 +1,31 @@ | ||
| // Implement a function isProperFraction, | ||
| // when given two numbers, a numerator and a denominator, it should return true if | ||
| // the given numbers form a proper fraction, and false otherwise. | ||
|
|
||
| // Assumption: The parameters are valid numbers (not NaN or Infinity). | ||
|
|
||
| // Note: If you are unfamiliar with proper fractions, please look up its mathematical definition. | ||
|
|
||
| // Acceptance criteria: | ||
| // After you have implemented the function, write tests to cover all the cases, and | ||
| // execute the code to ensure all tests pass. | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| // TODO: Implement this function | ||
| if (denominator === 0) return false; | ||
| 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; | ||
|
|
||
| // Here's our helper again | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
|
|
||
| // TODO: Write tests to cover all cases. | ||
| // What combinations of numerators and denominators should you test? | ||
|
|
||
| // Example: 1/2 is a proper fraction | ||
| // Proper fractions | ||
| assertEquals(isProperFraction(1, 2), true); | ||
| assertEquals(isProperFraction(2, 3), true); | ||
| assertEquals(isProperFraction(7, 10), true); | ||
|
|
||
| // Improper fractions | ||
| assertEquals(isProperFraction(3, 2), false); | ||
| assertEquals(isProperFraction(5, 5), false); | ||
|
|
||
| // Negative fractions (still proper) | ||
| assertEquals(isProperFraction(-1, 2), true); | ||
| assertEquals(isProperFraction(1, -2), true); | ||
| assertEquals(isProperFraction(-3, -4), true); | ||
|
|
||
| // Zero cases | ||
| assertEquals(isProperFraction(0, 2), true); | ||
| assertEquals(isProperFraction(1, 0), false); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,39 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| if (typeof card !== 'string') { | ||
| throw new Error('Invalid card: must be a string'); | ||
| } | ||
|
|
||
| const suits = ['♠', '♥', '♦', '♣']; | ||
| const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']; | ||
|
|
||
| let rank, suit; | ||
| if (card.length === 2) { | ||
| rank = card[0]; | ||
| suit = card[1]; | ||
| } else if (card.length === 3 && card.startsWith('10')) { | ||
| rank = '10'; | ||
| suit = card[2]; | ||
| } else { | ||
| throw new Error('Invalid card format'); | ||
| } | ||
|
|
||
| if (!suits.includes(suit)) { | ||
| throw new Error('Invalid suit'); | ||
| } | ||
|
|
||
| if (!ranks.includes(rank)) { | ||
| throw new Error('Invalid rank'); | ||
| } | ||
|
|
||
| if (rank === 'A') { | ||
| return 11; | ||
| } else if (['J', 'Q', 'K'].includes(rank)) { | ||
| return 10; | ||
| } else { | ||
| return parseInt(rank); | ||
| } | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -41,12 +73,53 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| // Test all ranks | ||
| assertEquals(getCardValue("A♠"), 11); | ||
| assertEquals(getCardValue("2♥"), 2); | ||
| assertEquals(getCardValue("3♦"), 3); | ||
| assertEquals(getCardValue("4♣"), 4); | ||
| assertEquals(getCardValue("5♠"), 5); | ||
| assertEquals(getCardValue("6♥"), 6); | ||
| assertEquals(getCardValue("7♦"), 7); | ||
| assertEquals(getCardValue("8♣"), 8); | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("10♥"), 10); | ||
| assertEquals(getCardValue("J♦"), 10); | ||
| assertEquals(getCardValue("Q♣"), 10); | ||
| assertEquals(getCardValue("K♠"), 10); | ||
|
|
||
| // Test different suits | ||
| assertEquals(getCardValue("A♥"), 11); | ||
| assertEquals(getCardValue("K♦"), 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) {} | ||
| } catch (e) { } | ||
|
|
||
| // Test invalid rank | ||
| try { | ||
|
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. It’s good that you’re thinking about invalid inputs as well as valid ones. One small improvement to think about: right now these checks only confirm that some error was thrown. Would it make the test more helpful if it also checked that the error explains the problem clearly?
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. Thanks for the suggestion! It makes sense. I’ve just updated the tests so they also check the error message to ensure the error clearly explains the problem with the input. |
||
| getCardValue("1♠"); | ||
| console.error("Error was not thrown for invalid rank"); | ||
| } catch (e) { } | ||
|
|
||
| // Test invalid suit | ||
| try { | ||
| getCardValue("A♤"); | ||
| console.error("Error was not thrown for invalid suit"); | ||
| } catch (e) { } | ||
|
|
||
| // Test wrong length | ||
| try { | ||
| getCardValue("A"); | ||
| console.error("Error was not thrown for wrong length"); | ||
| } catch (e) { } | ||
|
|
||
| // Test non-string | ||
| try { | ||
| getCardValue(123); | ||
| console.error("Error was not thrown for non-string"); | ||
| } catch (e) { } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
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.
Great job, your current implementation treats every negative fraction as false, and your Jest tests are written to match that. But what is the actual rule for a “proper fraction”?
It might be worth checking whether your implementation is based on the maths definition, or whether the tests and implementation have just been made consistent with each other.
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.
Thanks for pointing this out! I just checked the meaning of a proper fraction and realized that negative fractions can still be proper if the absolute value of the numerator is less than the absolute value of the denominator.
I’ve updated my implementation to use Math.abs() and adjusted the tests to include negative proper fractions as well.