Skip to content

Commit 2395603

Browse files
committed
fixing code based on mentor feedback
1 parent b1f57bf commit 2395603

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,35 @@
2222
// execute the code to ensure all tests pass.
2323
function getCardValue(card) {
2424
// TODO: Implement this function
25-
if (typeof card !== "string") {
25+
if (!card || typeof card !== "string") {
2626
throw new Error("Invalid card");
2727
}
28+
//
2829
const validSuits = ["♠", "♥", "♦", "♣"];
29-
const suit = card.slice(-1);
30-
const rank = card.slice(0, -1);
30+
const suit = card.slice(-1); //takes the last character of the string.
31+
const rank = card.slice(0, -1); //takes everything except the last character.
32+
3133
if (!validSuits.includes(suit)) {
32-
throw new Error("Invalid card");
33-
} else if (rank === "A") return 11;
34-
else if (["J", "Q", "K"].includes(rank)) return 10;
35-
const number = Number(rank);
36-
if (number >= 2 && number <= 10) return number;
34+
throw new Error("Invalid card"); // This if statement checks the suit is valid with the Array of suit we assigned
35+
}
36+
// Based on the question i get form mentor i changed the function if statement for better experience of the code.
37+
const cardValues = {
38+
A: 11,
39+
J: 10,
40+
Q: 10,
41+
K: 10,
42+
2: 2,
43+
3: 3,
44+
4: 4,
45+
5: 5,
46+
6: 6,
47+
7: 7,
48+
8: 8,
49+
9: 9,
50+
10: 10,
51+
}; // This is java script object that act as like lookup table for the valid card value input.
52+
53+
if (cardValues[rank] !== undefined) return cardValues[rank];
3754
throw new Error("Invalid card");
3855
}
3956

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ test(`should return "Invalid angles" when (angle >= 360 or angle<= 0 )`, () => {
4242
expect(getAngleType(0)).toBe("Invalid angle");
4343
expect(getAngleType(-45)).toBe("Invalid angle");
4444
expect(getAngleType(370)).toBe("Invalid angle");
45+
expect(getAngleType(360)).toBe("Invalid angle");
4546
});

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66
// Special case: numerator is zero
77
test(`should return false when denominator is zero`, () => {
8-
expect(isProperFraction(1, 0)).toEqual(false);
9-
expect(isProperFraction(0, 1)).toEqual(true);
8+
expect(isProperFraction(-11, 0)).toEqual(false);
9+
expect(isProperFraction(0, 0)).toEqual(false);
10+
expect(isProperFraction(7, 0)).toEqual(false);
11+
});
12+
// It should return false when the numerator > the denominator
13+
test("should return true when numerator is 0", () => {
1014
expect(isProperFraction(0, -1)).toEqual(true);
11-
expect(isProperFraction(18, 1)).toEqual(false);
12-
expect(isProperFraction(7, 3)).toEqual(false);
13-
expect(isProperFraction(1, -2)).toEqual(true);
14-
expect(isProperFraction(-15, -9)).toEqual(false);
15-
expect(isProperFraction(-2, -6)).toEqual(true);
16-
expect(isProperFraction(-137, -71)).toEqual(false);
17-
expect(isProperFraction(-100, -189)).toEqual(true);
15+
expect(isProperFraction(0, 3)).toEqual(true);
16+
expect(isProperFraction(0, -2)).toEqual(true);
17+
});
18+
test("should return true when numerator > denominator", () => {
19+
expect(isProperFraction(-12, -19)).toEqual(true);
20+
expect(isProperFraction(0, 6)).toEqual(true);
21+
expect(isProperFraction(17, -71)).toEqual(true);
22+
});
23+
test("should return false when numerator < denominator", () => {
24+
expect(isProperFraction(-180, -109)).toEqual(false);
1825
expect(isProperFraction(27, 5)).toEqual(false);
1926
expect(isProperFraction(-29, 17)).toEqual(false);
2027
});

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ test(`Should throw "Invalid card" for invalid cards`, () => {
2727
expect(() => getCardValue("B♠")).toThrow("Invalid card");
2828
expect(() => getCardValue("A$")).toThrow("Invalid card");
2929
expect(() => getCardValue("10X")).toThrow("Invalid card");
30+
expect(() => getCardValue("0x02♠")).toThrow("Invalid card");
31+
expect(() => getCardValue("2.1♠")).toThrow("Invalid card");
32+
expect(() => getCardValue("0002♠")).toThrow("Invalid card");
3033
});
3134
// Suggestion: Group the remaining test data into these categories:
3235
// Number Cards (2-10)

0 commit comments

Comments
 (0)