-
-
Notifications
You must be signed in to change notification settings - Fork 336
London | 26-ITP-Jan | Kris Oldrini | Sprint 3 | Implement And Rewrite Tests #1045
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 9 commits
5a0dfa0
f5555b2
c852657
ad52844
8a750dd
9fd83a0
9cc4680
e2a6053
8adf134
faa279a
2f0de76
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,7 +22,43 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
| if (typeof card !== "string") | ||
| throw new Error(`Invalid card format: "${card}". Expected a string.`); | ||
|
|
||
| let cardSuit = card.slice(-1); | ||
| let cardRank = card.slice(0, -1); | ||
|
|
||
| if (validSuits.includes(cardSuit)) { | ||
|
||
| switch (cardRank) { | ||
| case "A": | ||
| return 11; | ||
|
|
||
| case "J": | ||
| case "Q": | ||
| case "K": | ||
| return 10; | ||
|
|
||
| case "2": | ||
| case "3": | ||
| case "4": | ||
| case "5": | ||
| case "6": | ||
| case "7": | ||
| case "8": | ||
|
||
| case "9": | ||
| case "10": | ||
| return Number(cardRank); | ||
|
|
||
| default: | ||
| throw new Error( | ||
| `Invalid card format: "${card}" has an invalid rank "${cardRank}". Expected one of: A, 2–10, J, Q, K.` | ||
| ); | ||
| } | ||
| } else | ||
| throw new Error( | ||
| `Invalid card format: "${card}" has an invalid suit "${cardSuit}". Expected one of: ♠, ♥, ♦, ♣.` | ||
| ); | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -41,12 +77,147 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| assertEquals(getCardValue("4♣"), 4); | ||
|
|
||
| assertEquals(getCardValue("A♥"), 11); | ||
|
|
||
| assertEquals(getCardValue("J♦"), 10); | ||
|
|
||
| assertEquals(getCardValue("Q♣"), 10); | ||
|
|
||
| assertEquals(getCardValue("K♠"), 10); | ||
|
|
||
| assertEquals(getCardValue("K♦"), 10); | ||
|
|
||
| assertEquals(getCardValue("10♥"), 10); | ||
|
|
||
| assertEquals(getCardValue("2♦"), 2); | ||
|
|
||
| // 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 (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
|
|
||
| try { | ||
| getCardValue(""); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("1♥"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| 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 (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| 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 (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("4.8♣"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("G♦"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("9"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("K"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("9M"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("♠3"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue(6); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| 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 (err) { | ||
| console.error(err); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("♥"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,45 @@ 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`, () => { | ||
|
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. Is there a reason why this test was deleted? |
||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
|
|
||
| test(`should return true when the absolute value of the numerator is less than the absolute value of the denominator and the denominator is not 0`, () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(1, -3)).toEqual(true); | ||
| expect(isProperFraction(3.5, 4)).toEqual(true); | ||
| expect(isProperFraction(-99, -100)).toEqual(true); | ||
| expect(isProperFraction(8, 12.5)).toEqual(true); | ||
| expect(isProperFraction(-6, 12)).toEqual(true); | ||
| expect(isProperFraction(0.03, 0.1)).toEqual(true); | ||
| }); | ||
|
|
||
| test(`should return true when numerator is equal to 0 and denominator is either positive or negative`, () => { | ||
| expect(isProperFraction(0, -5)).toEqual(true); | ||
| expect(isProperFraction(0, 56)).toEqual(true); | ||
| expect(isProperFraction(0, 0.1)).toEqual(true); | ||
| expect(isProperFraction(0, -8.9)).toEqual(true); | ||
| }); | ||
|
|
||
| test(`should return false when the denominator is equal to zero`, () => { | ||
| expect(isProperFraction(-1, 0)).toEqual(false); | ||
| expect(isProperFraction(0, 0)).toEqual(false); | ||
| expect(isProperFraction(15, 0)).toEqual(false); | ||
| expect(isProperFraction(6.4, 0)).toEqual(false); | ||
| expect(isProperFraction(-8.5, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return false when the absolute value of the numerator is greater than the absolute value of the denominator`, () => { | ||
| expect(isProperFraction(3, 2)).toEqual(false); | ||
| expect(isProperFraction(-9, 3)).toEqual(false); | ||
| expect(isProperFraction(-15, -4)).toEqual(false); | ||
| expect(isProperFraction(36, -5)).toEqual(false); | ||
| expect(isProperFraction(3.8, 2.7)).toEqual(false); | ||
| expect(isProperFraction(1, 0.7)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return false when the absolute value of the numerator is equal to the absolute value of the denominator`, () => { | ||
| expect(isProperFraction(3, 3)).toEqual(false); | ||
| expect(isProperFraction(-19, -19)).toEqual(false); | ||
| expect(isProperFraction(7.45, 7.45)).toEqual(false); | ||
| expect(isProperFraction(67, -67)).toEqual(false); | ||
| expect(isProperFraction(-8, 8)).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.
This looks much cleaner after removing the logs. One small thing to think about: since cardSuit and cardRank aren't reassigned later in the function, is let the most appropriate declaration here?