-
-
Notifications
You must be signed in to change notification settings - Fork 239
NW | 25-ITP-Sep | TzeMing Ho | Sprint 3 | coursework/sprint-3-implement-and-rewrite #709
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 26 commits
9d43b8b
150a780
8fdb210
24f2d3c
96ebf89
b2f87c5
b73f73f
72112ca
a505558
6c1636f
401db2b
5733d12
096632e
b0fe035
7a2b0a1
096f9b6
5245e20
bc00dfc
bc1b331
51eedee
6c04424
3c8fe4f
6166064
4b1ad2d
892f63b
f14ee5c
76ad0cc
8b8773c
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,11 +8,42 @@ | |
| // Then, write the next test! :) Go through this process until all the cases are implemented | ||
|
|
||
| function getAngleType(angle) { | ||
| if ( | ||
|
||
| angle === null || | ||
| (typeof angle !== "number" && typeof angle !== "string") | ||
| ) { | ||
| return "Input should be a number or a number in string"; | ||
| } | ||
| angle = Number(angle); | ||
| if (isNaN(angle)) { | ||
| return "Input should be a number or a number in string"; | ||
| } | ||
| if (angle < 0 || angle > 360) { | ||
| return "Angle should be between 0 and 360"; | ||
| } | ||
| if (angle === 0) { | ||
| return "Zero angle"; | ||
| } | ||
| if (angle === 90) { | ||
| return "Right angle"; | ||
| } | ||
| // Run the tests, work out what Case 2 is testing, and implement the required code here. | ||
| // Then keep going for the other cases, one at a time. | ||
| // Run the tests, work out what Case 2 is testing, and implement the required code here. | ||
| // Then keep going for the other cases, one at a time. | ||
| if (angle < 90) { | ||
| return "Acute angle"; | ||
| } | ||
| if (angle > 90 && angle < 180) { | ||
| return "Obtuse angle"; | ||
| } | ||
| if (angle === 180) { | ||
| return "Straight angle"; | ||
| } | ||
| if (angle > 180 && angle < 360) { | ||
| return "Reflex angle"; | ||
| } | ||
| if (angle === 360) { | ||
| return "Full rotation"; | ||
| } | ||
| } | ||
|
|
||
| // The line below allows us to load the getAngleType function into tests in other files. | ||
|
|
@@ -51,13 +82,18 @@ assertEquals(acute, "Acute angle"); | |
| // Then the function should return "Obtuse angle" | ||
| const obtuse = getAngleType(120); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| assertEquals(obtuse, "Obtuse angle"); | ||
|
|
||
| // Case 4: Identify Straight Angles: | ||
| // When the angle is exactly 180 degrees, | ||
| // Then the function should return "Straight angle" | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| const straight = getAngleType(180); | ||
| assertEquals(straight, "Straight angle"); | ||
|
|
||
| // Case 5: Identify Reflex Angles: | ||
| // When the angle is greater than 180 degrees and less than 360 degrees, | ||
| // Then the function should return "Reflex angle" | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| const reflex = getAngleType(250); | ||
| assertEquals(reflex, "Reflex angle"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,13 @@ | |
| // write one test at a time, and make it pass, build your solution up methodically | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
|
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. Your isProperFraction works well. However, if you wanted to simplify it even more, you could try: |
||
| if (numerator < denominator) { | ||
| return true; | ||
| } | ||
| if (typeof numerator !== "number" && typeof numerator !== "string") | ||
| return false; | ||
| if (typeof denominator !== "number" && typeof denominator !== "string") | ||
| return false; | ||
| if (isNaN(numerator) || isNaN(denominator)) return false; | ||
| if (denominator == 0) return false; // handle zero denominator case | ||
| return Math.abs(numerator) < Math.abs(denominator); | ||
| } | ||
|
|
||
| // The line below allows us to load the isProperFraction function into tests in other files. | ||
|
|
@@ -47,13 +51,78 @@ assertEquals(improperFraction, false); | |
| // 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); | ||
|
|
||
| // 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); | ||
| // ====> complete with your assertion | ||
| assertEquals(equalFraction, false); | ||
|
|
||
| // Stretch: | ||
| // What other scenarios could you test for? | ||
|
|
||
| // Stretch 1: negative denominator | ||
| // Input: numerator = 2, denominator = -3 | ||
| // target output: true | ||
| // Explanation: The fraction 2/-3 is a proper fraction because the absolute value of denominator (3) is larger than the numerator (2). | ||
| const negativeDenominator = isProperFraction(2, -3); | ||
| assertEquals(negativeDenominator, true); | ||
|
|
||
| // Stretch 2: zero numerator | ||
| // Input: numerator = 0, denominator = 5 | ||
| // target output: true | ||
| // Explanation: The fraction 0/5 is a proper fraction because the absolute value of numerator (0) is less than the denominator (5). | ||
| const zeroNumerator = isProperFraction(0, 5); | ||
| assertEquals(zeroNumerator, true); | ||
|
|
||
| // Stretch 3: zero denominator - this is mathematically undefined but we can decide how we want to handle it | ||
| // Input: numerator = 5, denominator = 0 | ||
| // target output: false | ||
| // Explanation: The fraction 5/0 is undefined in mathematics. In this implementation, we choose to return false. | ||
| const zeroDenominator = isProperFraction(5, 0); | ||
| assertEquals(zeroDenominator, false); | ||
|
|
||
| // Stretch 4: both zero | ||
| // Input: numerator = 0, denominator = 0 | ||
| // target output: false | ||
| // Explanation: The fraction 0/0 is indeterminate in mathematics. In this implementation, we choose to return false. | ||
| const bothZero = isProperFraction(0, 0); | ||
| assertEquals(bothZero, false); | ||
|
|
||
| // Stretch 5: negative numerator and denominator | ||
| // Input: numerator = -3, denominator = -5 | ||
| // target output: true | ||
| // Explanation: The fraction -3/-5 is a proper fraction because the absolute value of the numerator (3) is less than the absolute value of the denominator (5). The function should return true. | ||
| const negativeNumeratorAndDenominator = isProperFraction(-3, -5); | ||
| assertEquals(negativeNumeratorAndDenominator, true); | ||
|
|
||
| // Stretch 6: improper negative numerator and denominator | ||
| // Input: numerator = -3, denominator = -2 | ||
| // target output: false | ||
| // Explanation: The fraction -3/-2 is an improper fraction because the absolute value of the numerator (3) is greater than the absolute value of the denominator (2). The function should return false. | ||
| const properNegativeNumeratorAndDenominator = isProperFraction(-3, -2); | ||
| assertEquals(properNegativeNumeratorAndDenominator, false); | ||
|
|
||
| // Stretch 7: decimal values | ||
| // Input: numerator = 2.5, denominator = 3.5 | ||
| // target output: true | ||
| // Explanation: The fraction 2.5/3.5 is a proper fraction because the absolute value of the numerator (2.5) is less than the absolute value of the denominator (3.5). The function should return true. | ||
| const decimalValues = isProperFraction(2.5, 3.5); | ||
| assertEquals(decimalValues, true); | ||
|
|
||
| // Stretch 8: improper decimal values | ||
| // Input: numerator = 3.5, denominator = 2.5 | ||
| // target output: false | ||
| // Explanation: The fraction 3.5/2.5 is an improper fraction because the absolute value of the numerator (3.5) is greater than the absolute value of the denominator (2.5). The function should return false. | ||
| const improperDecimalValues = isProperFraction(3.5, 2.5); | ||
| assertEquals(improperDecimalValues, false); | ||
|
|
||
| // Stretch 9: invalid inputs (non-numeric values) | ||
| // Input: numerator = "a", denominator = 2 | ||
| // target output: false | ||
| // Explanation: The function should handle non-numeric inputs gracefully. In this case, we choose to return false. | ||
| const invalidInputs = isProperFraction("a", 2); | ||
|
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. 👏 Good job making sure that 'unhappy path' is tested. |
||
| assertEquals(invalidInputs, false); | ||
|
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. When I run your tests, I get this result: Can you take a look? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,24 @@ | |
| // 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) { | ||
| const errorMessage = | ||
| "Error: Invalid card rank. Input should be a string in the format 'R♠', where R is 2-10, J, Q, K, or A, followed by either ♠, ♥, ♦, or ♣."; | ||
|
||
|
|
||
| if (typeof card !== "string" || card.length < 2) { | ||
| return errorMessage; | ||
| } | ||
| const rank = card.slice(0, -1); | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
| if (rank === "K" || rank === "Q" || rank === "J" || rank === "10") { | ||
| return 10; | ||
| } | ||
| const numericRank = Number(rank); | ||
| if (numericRank >= 2 && numericRank <= 9) { | ||
| return numericRank; | ||
| } | ||
| return errorMessage; | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -31,27 +46,44 @@ 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♥"); | ||
| const nineOfHearts = getCardValue("9♥"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| assertEquals(nineOfHearts, 9); | ||
|
|
||
| // 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 kingOfDiamonds = getCardValue("K♦"); | ||
| assertEquals(kingOfDiamonds, 10); | ||
| const queenOfClubs = getCardValue("Q♣"); | ||
| assertEquals(queenOfClubs, 10); | ||
| const jackOfHearts = getCardValue("J♥"); | ||
| assertEquals(jackOfHearts, 10); | ||
| const tenOfSpades = getCardValue("10♠"); | ||
| assertEquals(tenOfSpades, 10); | ||
|
|
||
| // Handle Ace (A): | ||
| // Given a card with a rank of "A", | ||
| // When the function is called with an Ace, | ||
| // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. | ||
| const aceOfHearts = getCardValue("A♥"); | ||
| assertEquals(aceOfHearts, 11); | ||
|
|
||
| // Handle Invalid Cards: | ||
| // 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." | ||
| const invalidCard = getCardValue("1♠"); | ||
| assertEquals(invalidCard, "Error: Invalid card rank"); | ||
| const anotherInvalidCard = getCardValue("Z♠"); | ||
| assertEquals(anotherInvalidCard, "Error: Invalid card rank"); | ||
| const emptyCard = getCardValue(""); | ||
| assertEquals(emptyCard, "Error: Invalid card rank"); | ||

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 would happen if I run the test with: