-
-
Notifications
You must be signed in to change notification settings - Fork 396
London | 26-ITP-May | Anita Amirhaeri | Sprint 3 | implement-and-rewrite #1568
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
684e9e3
31d9a17
f72b4fa
4b85410
18524d2
b5fc39c
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,8 +23,44 @@ | |
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| if (typeof card !== "string" || card.length < 2 || card.length > 3) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
| const ranks = [ | ||
| "A", | ||
| "2", | ||
| "3", | ||
| "4", | ||
| "5", | ||
| "6", | ||
| "7", | ||
| "8", | ||
| "9", | ||
| "10", | ||
| "J", | ||
| "Q", | ||
| "K", | ||
| ]; | ||
| const suits = ["♠", "♥", "♦", "♣"]; | ||
| let rank = card.slice(0, -1)// This is extracting the bit before the suits alone | ||
| let suit = card.slice(-1) // THis is extracting the suit in the string | ||
|
|
||
| if(!ranks.includes(rank)){ | ||
| throw new Error("Invalid card") | ||
| } | ||
| if (!suits.includes(suit)){ | ||
| throw new Error("Invalid card") | ||
| } | ||
| if (rank === "A"){ | ||
| return 11} | ||
|
Comment on lines
+45
to
+55
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. Indentation is off. Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode, |
||
| if (["J", "K", "Q"].includes(rank)){ | ||
| return 10 | ||
| } | ||
| else{ | ||
| return Number(rank) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // 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; | ||
|
|
@@ -40,6 +76,20 @@ function assertEquals(actualOutput, 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("K♦"), 10); | ||
| assertEquals(getCardValue("5♣"), 5); | ||
| assertEquals(getCardValue("2♣"), 2); | ||
| assertEquals(getCardValue("10♥"), 10); | ||
| assertEquals(getCardValue("6♥"), 6); | ||
| assertEquals(getCardValue("5♥"), 5); | ||
| assertEquals(getCardValue("8♥"), 8); | ||
| assertEquals(getCardValue("4♥"), 4); | ||
| assertEquals(getCardValue("3♥"), 3); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| assertEquals(getCardValue("7♥"), 7); | ||
|
|
||
|
|
||
| // Handling invalid cards | ||
| try { | ||
|
|
@@ -52,3 +102,17 @@ try { | |
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
|
|
||
| try { | ||
| getCardValue("A"); | ||
| console.error("Error not thrown for invalid card missing suit"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card missing suit🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue(""); | ||
| console.error("Error not thrown for invalid card containing empty string"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card containing empty string"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,21 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); | |
| test(`should return false when denominator is zero`, () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| }); | ||
| // proper fractions | ||
| test(`should return true when denominator is higher than numerator`, () => { | ||
| expect(isProperFraction(0, 1)).toEqual(true); | ||
| expect(isProperFraction(2, 7)).toEqual(true); | ||
| expect(isProperFraction(89, 101)).toEqual(true); | ||
| }); | ||
| // Special case: numerator or denominator is negative, we consider the absolute values for fractions so ignore the negative signs | ||
| test(`should return true when negative/positive numerator is less than the positive/negative denominator`, () => { | ||
| expect(isProperFraction(-20, -30)).toEqual(true); | ||
| expect(isProperFraction(-1, 2)).toEqual(true); | ||
| expect(isProperFraction(58, -68)).toEqual(true); | ||
| }); | ||
| // Special case: numerator or denominator is negative, we consider the absolute values for fractions so ignore the negative signs | ||
| test(`should return false when negative/positive numerator is greater than the positive/negative denominator`, () => { | ||
| expect(isProperFraction(-50, 10)).toEqual(false); | ||
| expect(isProperFraction(100, 2)).toEqual(false); | ||
| expect(isProperFraction(-1, -0)).toEqual(false); | ||
| }); | ||
|
Comment on lines
+12
to
+28
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.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,34 @@ | |
| const getCardValue = require("../implement/3-get-card-value"); | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all possible outcomes. | ||
|
|
||
| //["♠", "♥", "♦", "♣"]; | ||
| // Case 1: Ace (A) | ||
| test(`Should return 11 when given an ace card`, () => { | ||
| expect(getCardValue("A♠")).toEqual(11); | ||
| }); | ||
|
|
||
| //Case 2: Face Cards(J, Q,K) | ||
| test(`Should return 10 when given a face card`, () => { | ||
| expect(getCardValue("J♠")).toEqual(10); | ||
| expect(getCardValue("K♣")).toEqual(10); | ||
| expect(getCardValue("J♦")).toEqual(10); | ||
| }); | ||
| //case 3: Number Cards (2-10) | ||
| test(`Should return the number when given a number card`, () => { | ||
| expect(getCardValue("4♠")).toEqual(4); | ||
| expect(getCardValue("9♣")).toEqual(9); | ||
| expect(getCardValue("2♦")).toEqual(2); | ||
| }); | ||
| //Case 4: Invalid cards | ||
| test(`Should return the invalid suit, invalid card or invalid card`, () => { | ||
|
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. If the function is expected to throw an error, we could indicate so in the test description as:
|
||
| expect(() => getCardValue("Apple")).toThrow("Invalid card"); | ||
| expect(() => getCardValue("4🎉")).toThrow("Invalid card"); | ||
| expect(() => getCardValue("20♣")).toThrow("Invalid card"); | ||
| }); | ||
| // Suggestion: Group the remaining test data into these categories: | ||
| // Number Cards (2-10) | ||
| // Face Cards (J, Q, K) | ||
| // Invalid Cards | ||
|
|
||
| // To learn how to test whether a function throws an error as expected in Jest, | ||
| // please refer to the Jest documentation: | ||
| // https://jestjs.io/docs/expect#tothrowerror | ||
|
|
||
| // https://jestjs.io/docs/expecttothrowerror | ||
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.
Where did you find the specification of these two angles?