-
-
Notifications
You must be signed in to change notification settings - Fork 336
Birmingham | ITP-Jan | Ahmad Roman Sanaye | Sprint 3 | Implement functions and rewrite tests #1119
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 1 commit
f960b94
773ddc9
fc8717b
9d4539d
694f1cf
10c6347
431a4ef
9fc0d42
842dbaa
853bea5
f87ef80
14ffae5
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 |
|---|---|---|
|
|
@@ -12,6 +12,11 @@ | |
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| // TODO: Implement this function | ||
| if(numerator <= 0 || denominator <= 0){ | ||
| return false; | ||
| }else if(numerator < denominator){ | ||
|
||
| return true; | ||
| }else return false; | ||
| } | ||
|
|
||
| // The line below allows us to load the isProperFraction function into tests in other files. | ||
|
|
@@ -20,8 +25,7 @@ module.exports = isProperFraction; | |
|
|
||
| // Here's our helper again | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| console.assert(actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
|
|
@@ -30,4 +34,10 @@ function assertEquals(actualOutput, targetOutput) { | |
| // What combinations of numerators and denominators should you test? | ||
|
|
||
| // Example: 1/2 is a proper fraction | ||
| assertEquals(isProperFraction(1, 2), true); | ||
| assertEquals(isProperFraction(1, 2), "true"); | ||
| assertEquals(isProperFraction(3, 2), "false"); | ||
| assertEquals(isProperFraction(1, 0), "false"); | ||
| assertEquals(isProperFraction(8, 9), "true"); | ||
| assertEquals(isProperFraction(0, 1), "false"); | ||
| assertEquals(isProperFraction(-4, 1), "false"); | ||
| assertEquals(isProperFraction(2, -4), "false"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,25 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| } | ||
| if (typeof card !== "string") { | ||
| throw new Error("Invalid card"); | ||
| } | ||
| let rank = card.slice(0, -1); // Get everything except the last character | ||
| let suit = card.slice(-1); // Get the last character | ||
|
|
||
| const validSuits = ["♠", "♥", "♦", "♣"]; // check if suit is valid | ||
| if (!validSuits.includes(suit)) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| if (rank === "A"){ | ||
| return 11; | ||
| }else if(rank.match(/J|Q|K/)){ | ||
| return 10; | ||
| }else if(rank.match(/^(10|[2-9])$/)){ | ||
|
||
| return Number(rank); | ||
| }else throw new Error("Invalid card"); | ||
| } | ||
| // 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; | ||
|
|
@@ -36,17 +52,48 @@ function assertEquals(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("J♣"), 10); | ||
| assertEquals(getCardValue("Q♥"), 10); | ||
| assertEquals(getCardValue("K♠"), 10); | ||
| assertEquals(getCardValue("3♦"), 3); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| getCardValue("J"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| // The below line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| } catch (e) { | ||
| console.log('Test passed for "J": caught error ->', e.message); | ||
| } | ||
| // What other invalid card cases can you think of? | ||
|
|
||
| try { | ||
| getCardValue("9X"); // invalid suit | ||
| console.error('Test failed for "9X": error was not thrown'); | ||
| } catch (e) { | ||
| console.log('Test passed for "9X": caught error ->', e.message); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("1♠"); // invalid rank | ||
| console.error('Test failed for "1♠": error was not thrown'); | ||
| } catch (e) { | ||
| console.log('Test passed for "1♠": caught error ->', e.message); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("0♥"); // invalid rank | ||
| console.error('Test failed for "0♥": error was not thrown'); | ||
| } catch (e) { | ||
| console.log('Test passed for "0♥": caught error ->', e.message); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("ABC"); // completely wrong format | ||
| console.error('Test failed for "ABC": error was not thrown'); | ||
| } catch (e) { | ||
| console.log('Test passed for "ABC": caught error ->', e.message); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.