Skip to content

Commit 1a4482e

Browse files
committed
Updated after PR comments
1 parent 254abc7 commit 1a4482e

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function getCardValue(card) {
2727
let suit = "";
2828
let rank = card;
2929

30-
if (card.length > 1) {
30+
if (suits.includes(card.slice(-1))) {
3131
suit = card.slice(-1); // gives me the last character and saves it in the suit variable.
3232
rank = card.slice(0, -1); // gets beginning character up to (not including) the last character and saves it in the rank variable.
3333
}
@@ -42,15 +42,16 @@ function getCardValue(card) {
4242
}
4343

4444
// if card is face "J", "Q", "K"
45-
else if (rank === "J" || rank === "Q" || rank === "K") {
45+
if (rank === "J" || rank === "Q" || rank === "K") {
4646
return 10;
4747
}
4848

49+
const num = Number(rank);
4950
// if card is a number card and card is bigger than 2 and less than 10 ("2" to "10"), should return its numerical value
50-
else if (Number(rank) >= 2 && Number(rank) <= 10) {
51-
return Number(rank);
51+
if (rank !== String(num) || num < 2 || num > 10) {
52+
throw new Error("Error invalid card");
5253
}
53-
throw new Error("Error invalid card");
54+
return num;
5455
}
5556

5657
// The line below allows us to load the getCardValue function into tests in other files.
@@ -67,14 +68,14 @@ function assertEquals(actualOutput, targetOutput) {
6768

6869
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
6970
// Examples:
70-
assertEquals(getCardValue("9♠"), 9);
71+
// assertEquals(getCardValue("9♠"), 9);
7172

7273
// Handling invalid cards
73-
try {
74-
getCardValue("invalid");
74+
// try {
75+
// getCardValue("invalid");
7576

76-
// This line will not be reached if an error is thrown as expected
77-
console.error("Error was not thrown for invalid card");
78-
} catch (e) {}
77+
// // This line will not be reached if an error is thrown as expected
78+
// console.error("Error was not thrown for invalid card");
79+
// } catch (e) {}
7980

8081
// What other invalid card cases can you think of?

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test(`should return "Reflex angles" when (angle > 180 && < 360)`, () => {
3838
expect(getAngleType(306)).toEqual("Reflex angle");
3939
});
4040
// Case 6: Invalid angles
41-
test(`should return "Invalid angles" when (angle is =<0 &&>=360)`, () => {
41+
test(`should return "Invalid angles" when (angle is <= 0 && >= 360)`, () => {
4242
// Test various invalid angles, including boundary cases
4343
expect(getAngleType(400)).toEqual("Invalid angle");
4444
expect(getAngleType(-7)).toBe("Invalid angle");

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ test("should return false when numerator is bigger than denominator", () => {
2525

2626
// Case 4: Negative numbers
2727
// numerator or denominator is negative, but function compares absolute values
28-
test("should return correct result when numerator or denominator is negative", () => {
29-
expect(isProperFraction(-5, 9)).toEqual(true);
28+
test("should return false when |numerator| < |denominator| positive numerator and negative denominator)", () => {
3029
expect(isProperFraction(7, -3)).toEqual(false);
3130
});
31+
test("should return true when |numerator| < |denominator| negative numerator and positive denominator)", () => {
32+
expect(isProperFraction(-5, 9)).toEqual(true);
33+
});
3234

3335
// Case 5: Numerator equals denominator fraction equals 1, so it is not proper
3436
test("should return false when numerator is equal to denominator", () => {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ test(`Should return error if the card string is invalid`, () => {
3535
expect(() => getCardValue("17")).toThrow("Error invalid card");
3636
});
3737

38+
// Case 4: Hex, Decimal Numbers
39+
test(`Should return error if the rank is a hex input`, () => {
40+
expect(() => getCardValue("0x02♠")).toThrow("Error invalid card");
41+
expect(() => getCardValue("2.1♠")).toThrow("Error invalid card");
42+
expect(() => getCardValue("0002♠")).toThrow("Error invalid card");
43+
});
44+
3845
// To learn how to test whether a function throws an error as expected in Jest,
3946
// please refer to the Jest documentation:
4047
// https://jestjs.io/docs/expect#tothrowerror

0 commit comments

Comments
 (0)