-
-
Notifications
You must be signed in to change notification settings - Fork 398
West-Midlands | 26-ITP-May | Maryam Janjua | Sprint 3 | Implement and Rewrite Tests #1603
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
d321c48
d077e73
7500560
f3e36aa
5768ab4
c874085
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 |
|---|---|---|
|
|
@@ -23,6 +23,25 @@ | |
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| if(card=="A♠" || card=="A♥" || card=="A♦" || card == "A♣"){ | ||
| return 11; | ||
| } | ||
| if(card=="J♠" || card=="J♥" || card=="J♦" || card == "J♣" || | ||
| card=="Q♠" || card=="Q♥" || card=="Q♦" || card == "Q♣" || | ||
| card=="K♠" || card=="K♥" || card=="K♦" || card == "K♣" | ||
| ){ | ||
| return 10; | ||
| } | ||
|
Comment on lines
+26
to
+34
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. Note: Code works but could probably be simplified. |
||
|
|
||
| const rank =card.slice(0,-1); | ||
| const suit = card.slice(-1); | ||
| const validRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]; | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
| if(validRanks.includes(rank) && validSuits.includes(suit)){ | ||
| return Number(rank); | ||
| } | ||
| throw new Error("Invalid card"); | ||
|
|
||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -40,7 +59,12 @@ function assertEquals(actualOutput, targetOutput) { | |
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| assertEquals(getCardValue("K♥"), 10); | ||
| assertEquals(getCardValue("J♣"), 10); | ||
| assertEquals(getCardValue("A♦"), 11); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| assertEquals(getCardValue("10♠"), 10); | ||
| assertEquals(getCardValue("4♠"), 4); | ||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
|
|
@@ -50,5 +74,69 @@ try { | |
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| try { | ||
| getCardValue("11♠"); | ||
|
|
||
| // 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("Error thrown for invalid card 🎉"); | ||
| } | ||
| try { | ||
| getCardValue("0"); | ||
|
|
||
| // 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("Error thrown for invalid card 🎉"); | ||
| } | ||
| try { | ||
| getCardValue("-8♦"); | ||
|
|
||
| // 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("Error thrown for invalid card 🎉"); | ||
| } | ||
| try { | ||
| getCardValue("a♣"); | ||
|
|
||
| // 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("Error thrown for invalid card 🎉"); | ||
| } | ||
| try { | ||
| getCardValue("0x02♠"); | ||
| console.error("Error was not thrown for 0x02♠ 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for 0x02♠ 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("2.1♠"); | ||
| console.error("Error was not thrown for 2.1♠ 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for 2.1♠ 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("0002♠"); | ||
| console.error("Error was not thrown for 0002♠ 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for 0002♠ 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("KX"); | ||
| console.error("Error was not thrown for invalid suit 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid suit 🎉"); | ||
| } | ||
| try { | ||
| getCardValue("5X"); | ||
| console.error("Error was not thrown for invalid suit 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid suit 🎉"); | ||
| } | ||
|
cjyuan marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,44 @@ 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`, () => { | ||
| // Denominator is zero | ||
| test("should return false when denominator is zero", () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| expect(isProperFraction(-1, 0)).toEqual(false); | ||
| expect(isProperFraction(0, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| // Proper fractions with positive values | ||
| test("should return true when the absolute value of the numerator is less than the absolute value of the denominator", () => { | ||
|
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. Note: We could also use pseudo-code and math notations like |
||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(2, 4)).toEqual(true); | ||
| expect(isProperFraction(15, 30)).toEqual(true); | ||
| }); | ||
|
|
||
| // Numerator is zero | ||
| test("should return true when numerator is zero and denominator is non-zero", () => { | ||
| expect(isProperFraction(0, 9)).toEqual(true); | ||
| expect(isProperFraction(0, -9)).toEqual(true); | ||
| }); | ||
|
|
||
| // Equal numerator and denominator | ||
| test("should return false when numerator and denominator have equal absolute values", () => { | ||
| expect(isProperFraction(5, 5)).toEqual(false); | ||
| expect(isProperFraction(-5, -5)).toEqual(false); | ||
| }); | ||
|
|
||
| // Negative values that form proper fractions | ||
| test("should return true for negative values when the absolute numerator is less than the absolute denominator", () => { | ||
| expect(isProperFraction(-1, 2)).toEqual(true); | ||
| expect(isProperFraction(1, -2)).toEqual(true); | ||
| expect(isProperFraction(-1, -2)).toEqual(true); | ||
| }); | ||
|
|
||
| // Positive and negative values that do not form proper fractions | ||
| test("should return false when the absolute numerator is greater than or equal to the absolute denominator", () => { | ||
| expect(isProperFraction(-2, 1)).toEqual(false); | ||
| expect(isProperFraction(2, -1)).toEqual(false); | ||
| expect(isProperFraction(-2, -1)).toEqual(false); | ||
| expect(isProperFraction(6, 3)).toEqual(false); | ||
| expect(isProperFraction(10, 4)).toEqual(false); | ||
| }); | ||
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.
Why not use a formatter to keep the code consistently formatted?