-
-
Notifications
You must be signed in to change notification settings - Fork 395
London | 26-ITP-May | Damilola Odumosu| Sprint 3 | coursework #1528
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
4ea74f5
ecd12aa
a4ba8bc
822fcbc
1a96b7d
c727523
62aab55
09fe227
8270fd6
f092667
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 |
|---|---|---|
|
|
@@ -22,33 +22,104 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| // Basic validation: validating the input before slicing | ||
| if (card === "") { | ||
| throw new Error("No card was played") | ||
| } | ||
| if (card.length < 2 || card.length > 3) { | ||
| throw new Error("Invalid card played, rank and suit cannot be less than 1 or more than 3") | ||
| } | ||
|
Comment on lines
+26
to
+31
Contributor
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. What do "played" and "suit cannot be less than 1 or more than 3" mean? Are these checks necessary? |
||
| const rank = card.slice(0, card.length - 1).toUpperCase() | ||
| const suit = card.slice(card.length - 1) | ||
|
Comment on lines
+32
to
+33
Contributor
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.
|
||
|
|
||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
| const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; | ||
|
|
||
| // Suit and rank validation | ||
| if (!validSuits.includes(suit)) { | ||
| throw new Error("Invalid card played, suit is missing"); | ||
|
Contributor
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. The suit character may not be "missing". For examples, Could you think of a more general error message? |
||
| } | ||
| if (validRanks.includes(rank)) { | ||
| if (rank === "A") { | ||
| return 11; | ||
| } else if (rank === "J" || rank === "Q" || rank === "K") { | ||
| return 10; | ||
| } else { | ||
| return Number(rank); | ||
| } | ||
| } else { | ||
| throw new Error("Invalid rank"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = getCardValue; | ||
|
|
||
| // Helper functions to make our assertions easier to read. | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${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("A♥"), 11); | ||
| assertEquals(getCardValue("A♦"), 11); | ||
| 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); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| getCardValue(""); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| } catch (e) { | ||
| console.log(e); | ||
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| try { | ||
| getCardValue("100"); | ||
| console.error("Error was not thrown for card with more than 3 in length"); | ||
| } catch (e) { | ||
| console.log(e) | ||
| } | ||
| try { | ||
| getCardValue("1") | ||
| console.error("Error was not thrown for a card.lenght = 1") | ||
| } catch (e) { | ||
| console.log(e) | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("♦") | ||
| console.error("Error was not thrown for a card play of just suits") | ||
| } catch (e) { | ||
| console.log(e) | ||
| } | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| try { | ||
| getCardValue("A😊") | ||
| console.error("Error was not thrown for a card play of a wrong suit") | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| console.log(e) | ||
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| // We will use the same function, but write tests for it using Jest in this file. | ||
| const getAngleType = require("../implement/1-get-angle-type"); | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all cases/outcomes, | ||
| // including boundary and invalid cases. | ||
|
|
||
| // Case 1: Acute angles | ||
|
|
@@ -13,8 +12,30 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { | |
| expect(getAngleType(89)).toEqual("Acute angle"); | ||
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| // Case 3: Obtuse angles | ||
|
|
||
| // Case 2: Obtuse angle | ||
| test(`should return "obtuse angle" when (90 < angle < 180 `, () =>{ | ||
| expect(getAngleType(91)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(179)).toEqual("Obtuse angle"); | ||
|
|
||
| }) | ||
| // Case 3: Right angles | ||
| test(`should return "Right angle" when (angle === 90)`, () => { | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }) | ||
|
|
||
| // Case 4: Straight angle | ||
| test(`should return "Straight angle" when (angle === 180)`, () => { | ||
| expect(getAngleType(180)).toEqual("Straight angle") | ||
| }) | ||
| // Case 5: Reflex angles | ||
| test(`should return "Reflect angle" when (180 < angle < 360))`, () => { | ||
| expect(getAngleType(181)).toEqual("Reflex angle"); | ||
| expect(getAngleType(359)).toEqual("Reflex angle"); | ||
| }) | ||
| // Case 6: Invalid angles | ||
| test(`should return "Invalid angle" when (0 > angle > 360)`, () => { | ||
|
Contributor
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. The notation |
||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(-1)).toEqual("Invalid angle"); | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,36 @@ | |
| // We will use the same function, but write tests for it using Jest in this file. | ||
| const isProperFraction = require("../implement/2-is-proper-fraction"); | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. | ||
|
|
||
| // Special case: numerator is zero | ||
| test(`should return false when denominator is zero`, () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| test("should correctly identify proper fractions", () => { | ||
|
Contributor
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. Could probably break this test category into several more specific test categories. |
||
| // Whole number fractions | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(2, 1)).toEqual(false); | ||
| expect(isProperFraction(5, 5)).toEqual(false); | ||
|
|
||
| // Zero | ||
| expect(isProperFraction(0, 5)).toEqual(true); | ||
| expect(isProperFraction(5, 0)).toEqual(false); | ||
| expect(isProperFraction(0, 0)).toEqual(false); | ||
|
|
||
| // Negative numbers | ||
| expect(isProperFraction(-1, 2)).toEqual(true); | ||
| expect(isProperFraction(1, -2)).toEqual(false); | ||
| expect(isProperFraction(-2, -1)).toEqual(true); | ||
| expect(isProperFraction(-1, -2)).toEqual(false); | ||
|
|
||
| // Decimal numbers | ||
| expect(isProperFraction(0.5, 1)).toEqual(true); | ||
| expect(isProperFraction(1.5, 1)).toEqual(false); | ||
|
|
||
| // Infinity | ||
| expect(isProperFraction(Infinity, 2)).toEqual(false); | ||
| expect(isProperFraction(2, Infinity)).toEqual(false); | ||
|
|
||
| // NaN | ||
| expect(isProperFraction(NaN, 2)).toEqual(false); | ||
| expect(isProperFraction(2, NaN)).toEqual(false); | ||
|
|
||
| // Large numbers | ||
| expect(isProperFraction(999999999, 1000000000)).toEqual(true); | ||
| }); | ||
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.
What authoritative source do you base your definition of a proper fraction on?
Code is not consistently formatted.
Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md ?