Skip to content

Commit 7d4b528

Browse files
committed
Made changes based on comments received.
1 parent 1ba0eff commit 7d4b528

4 files changed

Lines changed: 49 additions & 22 deletions

File tree

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
if (denominator === 0) {
15-
return false;
16-
} else if (Math.abs(numerator) < Math.abs(denominator)) {
17-
return true;
18-
} else {
19-
return false;
20-
}
14+
return Math.abs(numerator) < Math.abs(denominator) ? true : false;
2115
}
2216

2317
// The line below allows us to load the isProperFraction function into tests in other files.

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
const rank = card.slice(0, card.length - 1);
26-
if (rank === "A") {
27-
return 11;
28-
} else if (Number(rank) >= 2 && Number(rank) < 10) {
29-
return Number(rank);
30-
} else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
31-
return 10;
32-
} else {
33-
return "Invalid card rank";
25+
const rank = card.slice(0, -1);
26+
const lastChar = card.slice(-1);
27+
const validSuits = ["♠", "♥", "♦", "♣"];
28+
29+
if (!validSuits.includes(lastChar)) {
30+
throw new Error("Invalid card suit");
31+
}
32+
33+
if (rank === "A") return 11;
34+
if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") return 10;
35+
36+
const rankNum = Number(rank);
37+
if (!Number.isNaN(rankNum) && rankNum >= 2 && rankNum < 10) {
38+
return rankNum;
3439
}
40+
41+
throw new Error("Invalid card rank");
3542
}
3643

3744
// The line below allows us to load the getCardValue function into tests in other files.
@@ -76,5 +83,8 @@ assertEquals(aceofClubs, 11);
7683
// Given a card with an invalid rank (neither a number nor a recognized face card),
7784
// When the function is called with such a card,
7885
// Then it should throw an error indicating "Invalid card rank."
79-
const invalidCard = getCardValue("1♠");
80-
assertEquals(invalidCard, "Invalid card rank");
86+
try {
87+
getCardValue("1♠");
88+
} catch (error) {
89+
assertEquals(error.message, "Invalid card rank");
90+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,19 @@ test(`should return true for numerator zero and positive denominator`, () => {
2424
test(`should return false for denominator zero`, () => {
2525
expect(isProperFraction(5, 0)).toEqual(false);
2626
});
27+
28+
test(`should return false for negative denominator`, () => {
29+
expect(isProperFraction(5, -2)).toEqual(false);
30+
});
31+
32+
test(`should return false for negative numerator`, () => {
33+
expect(isProperFraction(-5, 2)).toEqual(false);
34+
});
35+
36+
test(`should return false for negative numerator`, () => {
37+
expect(isProperFraction(-5, 2)).toEqual(false);
38+
});
39+
40+
test(`should return true for negative numerator and negative denominator where numerator is smaller.`, () => {
41+
expect(isProperFraction(-1, -2)).toEqual(true);
42+
});

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ test(`Should return 10 for face cards`, () => {
2929
});
3030

3131
// Case 4: Invalid Cards
32-
test(`Should return Invalid card rank for invalid cards`, () => {
33-
expect(getCardValue("1♠")).toEqual("Invalid card rank");
34-
expect(getCardValue("11♥")).toEqual("Invalid card rank");
35-
expect(getCardValue("Z♦")).toEqual("Invalid card rank");
32+
test(`Should throw an error for Invalid card rank for invalid cards`, () => {
33+
expect(() => getCardValue("1♠")).toThrow("Invalid card rank");
34+
expect(() => getCardValue("11♥")).toThrow("Invalid card rank");
35+
expect(() => getCardValue("Z♦")).toThrow("Invalid card rank");
36+
});
37+
38+
// Case 5: Invalid suit character
39+
test(`Should throw an error for invalid suit character`, () => {
40+
expect(() => getCardValue("1*")).toThrow("Invalid card suit");
41+
expect(() => getCardValue("11-")).toThrow("Invalid card suit");
42+
expect(() => getCardValue("Z/")).toThrow("Invalid card suit");
3643
});
3744

3845
// To learn how to test whether a function throws an error as expected in Jest,

0 commit comments

Comments
 (0)