Skip to content

Commit 865cf09

Browse files
committed
seperated suit and refactor function
1 parent 815a1c8 commit 865cf09

File tree

5 files changed

+82
-72
lines changed

5 files changed

+82
-72
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
// Acceptance criteria:
1010
// After you have implemented the function, write tests to cover all the cases, and
1111
// execute the code to ensure all tests pass.
12-
1312
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
15-
if ( Math.abs(numerator) >= Math.abs(denominator)) {
13+
if (denominator === 0) {
1614
return false;
17-
} else if (Math.abs(denominator) === 0) {
15+
}
16+
17+
if (Math.abs(numerator) >= Math.abs(denominator)) {
1818
return false;
19-
} else {
20-
return true;
2119
}
20+
21+
return true;
2222
}
2323

2424
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -50,7 +50,3 @@ assertEquals(isProperFraction(1, -2), true);
5050
assertEquals(isProperFraction(-1, 2), true);
5151
assertEquals(isProperFraction(1, 0), false);
5252
assertEquals(isProperFraction(-1, 0), false);
53-
54-
55-
56-

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

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,44 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
if (card.length >= 4) {
26-
throw new Error ("Invalid String");
27-
}
28-
if (
29-
!card.includes("♠") &&
30-
!card.includes("♥") &&
31-
!card.includes("♦") &&
32-
!card.includes("♣")
33-
) {
34-
throw new Error('Invalid String');
35-
}
36-
const rank = card.slice(0, -1);
37-
3825
if (
39-
!card.includes("A") &&
40-
!card.includes("J") &&
41-
!card.includes("Q") &&
42-
!card.includes("K") &&
43-
!(rank >= 2 && rank <= 10)
26+
typeof card !== "string" ||
27+
card.length < 2 ||
28+
card.length > 3 ||
29+
card.includes(" ")
4430
) {
45-
throw new Error("Invalid String");
31+
throw new Error("Invalid string");
4632
}
4733

48-
if (rank === "A") {
34+
const suit = card.slice(-1);
35+
const rank = card.slice(0, -1);
36+
const validSuits = ["♠", "♥", "♦", "♣"];
37+
const validRanks = [
38+
"A",
39+
"J",
40+
"Q",
41+
"K",
42+
"2",
43+
"3",
44+
"4",
45+
"5",
46+
"6",
47+
"7",
48+
"8",
49+
"9",
50+
"10",
51+
];
52+
53+
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
54+
throw new Error("Invalid string");
55+
} else if (rank === "A") {
4956
return 11;
5057
} else if (rank === "J" || rank === "Q" || rank === "K") {
5158
return 10;
52-
} else if (rank >= 2 && rank <= 10) {
59+
} else if (rank) {
5360
return Number(rank);
5461
}
62+
throw new Error("Invalid string");
5563
}
5664

5765
// The line below allows us to load the getCardValue function into tests in other files.
@@ -76,18 +84,15 @@ assertEquals(getCardValue("Q♠"), 10);
7684
assertEquals(getCardValue("2♠"), 2);
7785
assertEquals(getCardValue("10♠"), 10);
7886

79-
80-
8187
// Handling invalid cards
82-
function throwInvalidStringError(card){
83-
84-
try {
85-
getCardValue(card)
86-
// This line will not be reached if an error is thrown as expected
87-
console.error("Error was not thrown for invalid card");
88-
} catch (e) {
89-
assertEquals(e.message,"Invalid String")
90-
}
88+
function throwInvalidStringError(card) {
89+
try {
90+
getCardValue(card);
91+
// This line will not be reached if an error is thrown as expected
92+
console.error("Error was not thrown for invalid card");
93+
} catch (e) {
94+
assertEquals(e.message, "Invalid string");
95+
}
9196
}
9297

9398
throwInvalidStringError("invalid");
@@ -102,6 +107,10 @@ throwInvalidStringError("2.1♠");
102107
throwInvalidStringError("0002♠");
103108
throwInvalidStringError("22");
104109
throwInvalidStringError("♠2");
110+
throwInvalidStringError("2 ♠");
111+
throwInvalidStringError("2.♠");
112+
throwInvalidStringError("+2♠");
113+
throwInvalidStringError("♠A");
105114

106115
// What other invalid card cases can you think of?
107116
// card with a mix of suit

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
@@ -34,7 +34,7 @@ test(`should return "Straight angle" when (angle===180)`, () => {
3434
});
3535

3636
// Case 5: Reflex angles
37-
test(`Should return "Reflex angle" when (360<angle>180)`, () => {
37+
test(`Should return "Reflex angle" when (angle > 180 && angle < 360)`, () => {
3838
expect(getAngleType(181)).toEqual("Reflex angle");
3939
expect(getAngleType(270)).toEqual("Reflex angle");
4040
expect(getAngleType(350)).toEqual("Reflex angle");

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ 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

77
// Special case: numerator is zero
8-
test(`should return false when denominator is zero`, () => {
8+
test(`should return true when numerator is zero`, () => {
9+
expect(isProperFraction(0, -4)).toEqual(true);
10+
expect(isProperFraction(0, 1)).toEqual(true);
11+
});
12+
test("should return false when denominator is zero", () => {
913
expect(isProperFraction(1, 0)).toEqual(false);
10-
expect(isProperFraction(1, -1)).toEqual(false);
11-
expect(isProperFraction(10, 1)).toEqual(false);
12-
expect(isProperFraction(1, 10)).toEqual(true);
14+
expect(isProperFraction(10, 0)).toEqual(false);
15+
expect(isProperFraction(-2, 0)).toEqual(false);
1316
expect(isProperFraction(-1, 0)).toEqual(false);
14-
expect(isProperFraction(1e2, 1e4)).toEqual(true);
15-
expect(isProperFraction(1e4, 1e0)).toEqual(false);
17+
expect(isProperFraction(1, 0)).toEqual(false);
1618
});

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ test(`Should return 11 when given an ace card`, () => {
1111

1212
// Suggestion: Group the remaining test data into these categories:
1313
// Number Cards (2-10)
14-
test(`should return Number from "2-10" when given a number card`,()=>{
15-
14+
test(`should return Number from "2-10" when given a number card`, () => {
1615
expect(getCardValue("2♠")).toEqual(2);
1716
expect(getCardValue("3♠")).toEqual(3);
1817
expect(getCardValue("4♣")).toEqual(4);
@@ -22,34 +21,38 @@ test(`Should return 11 when given an ace card`, () => {
2221
expect(getCardValue("8♠")).toEqual(8);
2322
expect(getCardValue("9♠")).toEqual(9);
2423
expect(getCardValue("10♠")).toEqual(10);
25-
})
26-
24+
});
2725

2826
// Face Cards (J, Q, K)
2927

30-
test(`Should return 10 when given a face card`,()=>{
31-
32-
expect(getCardValue("J♣")).toEqual(10);
33-
expect(getCardValue("K♣")).toEqual(10);
34-
expect(getCardValue("Q♣")).toEqual(10);
35-
expect(getCardValue("Q♠")).toEqual(10);
36-
expect(getCardValue("J♠")).toEqual(10);
37-
expect(getCardValue("K♠")).toEqual(10);
38-
})
28+
test(`Should return 10 when given a face card`, () => {
29+
expect(getCardValue("J♣")).toEqual(10);
30+
expect(getCardValue("K♣")).toEqual(10);
31+
expect(getCardValue("Q♣")).toEqual(10);
32+
expect(getCardValue("Q♠")).toEqual(10);
33+
expect(getCardValue("J♠")).toEqual(10);
34+
expect(getCardValue("K♠")).toEqual(10);
35+
});
3936
// Invalid Cards
4037

41-
test('throws on Invalid String', () => {
42-
expect(() => { getCardValue('A10♠');}).toThrow("Invalid String");
43-
expect(() => { getCardValue('A');}).toThrow("Invalid String");
44-
expect(() => { getCardValue('110♠');}).toThrow("Invalid String");
45-
expect(() => { getCardValue(' ♠');}).toThrow("Invalid String");
46-
expect(() => { getCardValue('eeeeeej1234');}).toThrow("Invalid String");
47-
38+
test("throws on Invalid String", () => {
39+
expect(() => {
40+
getCardValue("A10♠");
41+
}).toThrow("Invalid string");
42+
expect(() => {
43+
getCardValue("A");
44+
}).toThrow("Invalid string");
45+
expect(() => {
46+
getCardValue("110♠");
47+
}).toThrow("Invalid string");
48+
expect(() => {
49+
getCardValue(" ♠");
50+
}).toThrow("Invalid string");
51+
expect(() => {
52+
getCardValue("eeeeeej1234");
53+
}).toThrow("Invalid string");
4854
});
4955

50-
51-
5256
// To learn how to test whether a function throws an error as expected in Jest,
5357
// please refer to the Jest documentation:
5458
// https://jestjs.io/docs/expect#tothrowerror
55-

0 commit comments

Comments
 (0)