-
-
Notifications
You must be signed in to change notification settings - Fork 239
Birmingham | 25-ITP-Sep | Ahmad Ehsas | Sprint 3 | Implement-and-rewrite-tests #735
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
65fe894
532fd66
0cba6bb
8d6eb95
2479c4f
3fafedf
7a5fe08
68f5551
5dd3b88
62ef41d
1443cbf
1e2318b
edea881
dbf4e92
fad2100
1154f89
047df5d
ba0f73e
5aeb5c0
550f44d
e76a59c
3415822
bf5f7f8
4f1e86a
e5699a5
63d4861
e749f57
d34b464
fb90283
8b3fd7a
b1e528f
e956cdc
c57fbd0
7490201
154071d
30dfbd8
a0cf3ae
56ccaef
5c8aac3
8accc29
5b0a96e
3b28213
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 |
|---|---|---|
|
|
@@ -8,9 +8,14 @@ | |
| // write one test at a time, and make it pass, build your solution up methodically | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| if (numerator < denominator) { | ||
| return true; | ||
| } | ||
| if (Math.abs(numerator) < Math.abs(denominator)) return true; // This version of code works correctly for proper and negative fractions. | ||
| if (Math.abs(numerator) >= Math.abs(denominator)) return false; | ||
| if (Math.abs(numerator) === Math.abs(denominator)) return false; | ||
|
Comment on lines
+11
to
+13
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.
|
||
|
|
||
| // if (numerator < denominator) { | ||
| // return true; | ||
| // } | ||
|
|
||
| } | ||
|
|
||
| // The line below allows us to load the isProperFraction function into tests in other files. | ||
|
|
@@ -46,14 +51,21 @@ assertEquals(improperFraction, false); | |
| // target output: true | ||
| // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| // ====> complete with your assertion | ||
| assertEquals(negativeFraction, true); // assertion for negative fraction. | ||
|
|
||
| // Equal Numerator and Denominator check: | ||
| // Input: numerator = 3, denominator = 3 | ||
| // target output: false | ||
| // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. | ||
| const equalFraction = isProperFraction(3, 3); | ||
| assertEquals(equalFraction, false); // assertion for equal numerator and denominator. | ||
| // ====> complete with your assertion | ||
|
|
||
| // Stretch: | ||
| // What other scenarios could you test for? | ||
| console.log(isProperFraction(5, 2)); | ||
| console.log(isProperFraction(-4, 7)); | ||
| console.log(isProperFraction(-3, 3)); | ||
| console.log(isProperFraction(-2, -3)); // This is a proper fraction because the absolute value of numerator 2 and denominator is 3. | ||
| // the value of numerator is less than denominator. | ||
| console.log(isProperFraction(-4, -7)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,15 +8,80 @@ | |
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
| function getCardValue(card) { | ||
| if (rank === "A") { | ||
| return 11; | ||
| if (typeof card !== "string") { | ||
| throw new Error("Card must be a string") | ||
| } | ||
| } | ||
|
|
||
| if (card.length < 2) { | ||
| throw new Error("Invalid card rank") | ||
| } | ||
|
|
||
| const rank = card.slice(0, -1); | ||
| const suit = card.slice(-1); | ||
|
|
||
| if (!["♠", "♥", "♦", "♣"].includes(suit)) { | ||
| throw new Error("Invalid card rank"); | ||
| } | ||
|
|
||
| if (rank === "A") return 11; // Ace | ||
|
|
||
|
|
||
|
|
||
| //const rank = card.slice(0, -1); // get the rank (before the suit symbol) | ||
|
|
||
|
|
||
| // const num = Number(rank); // converting to a number. | ||
|
|
||
| // if (!isNaN(rank)) { | ||
| // return num; // Number card | ||
| // } | ||
|
|
||
| if (rank === "J" || rank === "Q" || rank === "K") { | ||
| return 10; // Face cards | ||
| } | ||
|
|
||
| if (!/^[0-9]+$/.test(rank)) { | ||
| throw new Error("Invalid card rank"); | ||
| } | ||
|
Comment on lines
+43
to
+45
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 is a stricter check. Still, any string containing only digits could pass this test. For example, "0000002". |
||
| // Reject weird or malformed ranks (like 3AAAA) | ||
|
|
||
| const num = Number(rank); | ||
| // Convert numeric string | ||
|
|
||
| if (num >= 2 && num <= 10) { | ||
| return num; | ||
| } | ||
| // Accept valid 2–10 range only | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // Anything else is invalid | ||
| throw new Error("Invalid card rank") | ||
|
|
||
| } | ||
|
|
||
|
|
||
| console.log(Number("0x002")); | ||
| console.log(Number("2.1")); | ||
| console.log(Number("9e1")); | ||
| console.log(Number("0002")); | ||
|
|
||
|
|
||
|
|
||
| // if the rank is not a number or a face card, throw an error(invalid card rank). | ||
|
|
||
| // if (rank === "x") { | ||
| // return 11; | ||
| // } | ||
|
|
||
|
|
||
|
|
||
| // 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; | ||
|
|
||
|
|
||
| // You need to write assertions for your function to check it works in different cases | ||
| // we're going to use this helper function to make our assertions easier to read | ||
| // if the actual output matches the target output, the test will pass | ||
|
|
@@ -31,21 +96,31 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A), | ||
| // When the function getCardValue is called with this card string as input, | ||
| // Then it should return the numerical card value | ||
| const aceofSpades = getCardValue("A♠"); | ||
| assertEquals(aceofSpades, 11); | ||
| const aceOfSpades = getCardValue("A♠"); | ||
| assertEquals(aceOfSpades, 11); | ||
|
|
||
| // Handle Number Cards (2-10): | ||
| // Given a card with a rank between "2" and "9", | ||
| // When the function is called with such a card, | ||
| // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| const fiveOfHearts = getCardValue("5♥"); | ||
| assertEquals(fiveOfHearts, 5); | ||
|
|
||
| const tenOfDiamonds = getCardValue("10♦"); | ||
| assertEquals(tenOfDiamonds, 10); | ||
|
|
||
| // Handle Face Cards (J, Q, K): | ||
| // Given a card with a rank of "10," "J," "Q," or "K", | ||
| // When the function is called with such a card, | ||
| // Then it should return the value 10, as these cards are worth 10 points each in blackjack. | ||
| const jackOfClubs = getCardValue("J♣"); | ||
| assertEquals(jackOfClubs, 10); | ||
|
|
||
| const queenOfHearts = getCardValue("Q♥"); | ||
| assertEquals(queenOfHearts, 10); | ||
|
|
||
| const kingOfSpades = getCardValue("K♠"); | ||
| assertEquals(kingOfSpades, 10); | ||
| // Handle Ace (A): | ||
| // Given a card with a rank of "A", | ||
| // When the function is called with an Ace, | ||
|
|
@@ -55,3 +130,8 @@ const fiveofHearts = getCardValue("5♥"); | |
| // Given a card with an invalid rank (neither a number nor a recognized face card), | ||
| // When the function is called with such a card, | ||
| // Then it should throw an error indicating "Invalid card rank." | ||
| try { | ||
| getCardValue("Z♠"); | ||
| } catch (error) { | ||
| console.log("Caught error:", error.message); // Should say "Invalid card rank" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,21 @@ test("should return true for a proper fraction", () => { | |
| }); | ||
|
|
||
| // Case 2: Identify Improper Fractions: | ||
| test("should return false for improper fractions", () => { | ||
| const improperFraction = isProperFraction(5, 2); | ||
| expect(improperFraction).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 3: Identify Negative Fractions: | ||
| test("should return true for negative fractions", () => { | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| expect(negativeFraction).toEqual(true); | ||
| }); | ||
|
|
||
|
Comment on lines
+16
to
+20
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. Not all negative fractions are proper fractions. For example, -5/2. If you allow negative denominator, then you could also test 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 employed 'Math.abs(denominator)' to handle fractions with negative denominators. 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 function is correct. I was referring to test and the test description. |
||
|
|
||
| // Case 4: Identify Equal Numerator and Denominator: | ||
| test("should return false for equal numerator and denominator", () => { | ||
| const equalFraction = isProperFraction(3, 3); | ||
| expect(equalFraction).toEqual(false); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,58 @@ | |
| // We will use the same function, but write tests for it using Jest in this file. | ||
| const getCardValue = require("../implement/3-get-card-value"); | ||
|
|
||
|
|
||
| // Case 4: Handle Ace (A): | ||
| test("should return 11 for Ace of Spades", () => { | ||
| const aceofSpades = getCardValue("A♠"); | ||
| expect(aceofSpades).toEqual(11); | ||
| }); | ||
|
|
||
| // Case 2: Handle Number Cards (2-10): | ||
| test("should return correct values for number cards", () => { | ||
| expect(getCardValue("5♥")).toEqual(5); | ||
| expect(getCardValue("10♦")).toEqual(10); | ||
| }); | ||
|
|
||
|
|
||
| // Case 3: Handle Face Cards (J, Q, K): | ||
| // Case 4: Handle Ace (A): | ||
| test("should return for face cards J, Q, K", () => { | ||
| expect(getCardValue("J♣")).toEqual(10); | ||
| expect(getCardValue("Q♦")).toEqual(10); | ||
| expect(getCardValue("K♠")).toEqual(10); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
| // Case 5: Handle Invalid Cards: | ||
| test("throws an error or invalid card", () => { | ||
| expect(() => getCardValue("Z♠")).toThrow("Invalid card"); | ||
| }); | ||
|
|
||
| test("should throw error for malformed numeric input", () => { | ||
| expect(() => getCardValue("2.9999♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for repeated characters in rank", () => { | ||
| expect(() => getCardValue("3AAAA♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for number beyond valid range", () => { | ||
| expect(() => getCardValue("11♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for missing suit", () => { | ||
| expect(() => getCardValue("Q")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for non-string input", () => { | ||
| expect(() => getCardValue(5)).toThrow("Card must be a string"); | ||
| }); | ||
|
|
||
|
Comment on lines
+29
to
+52
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.
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. Why specifically test "repeated characters" in rank? What's so special about this particular string pattern? |
||
| try { | ||
| getCardValue("Z♠"); | ||
| } catch (error) { | ||
| console.log("Caught error:", error.message); | ||
| } | ||
|
|
||
|
|
||
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.
The subfolder containing these two files were moved from its original folder.