Skip to content

Commit 2a9ffe9

Browse files
committed
reviewed errors fixed
1 parent 04a8f07 commit 2a9ffe9

3 files changed

Lines changed: 56 additions & 27 deletions

File tree

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,39 @@
2323

2424
function getCardValue(card) {
2525
// Handling invalid cards
26-
try{
27-
const rank = card.slice(0,-1); //rank of the card is everything except the last character of card string
28-
const suit = card.slice(-1); // suit is the last character of the card string
29-
if((card.length != 2 && card.length !=3) || !isValidCard(rank, suit)){
30-
throw new Error("Invalid card");
31-
}
32-
if(rank === "J" || rank === "Q" || rank == "K")
33-
return 10
34-
else if(rank == "A")
35-
return 11;
36-
else
37-
return Number(rank);
38-
}
39-
catch(e){
26+
try {
27+
const rank = card.slice(0, -1); //rank of the card is everything except the last character of card string
28+
const suit = card.slice(-1); // suit is the last character of the card string
29+
if (!isValidCard(rank, suit)) {
30+
throw new Error("Invalid card");
31+
}
32+
if (rank === "J" || rank === "Q" || rank == "K") return 10;
33+
else if (rank == "A") return 11;
34+
else return Number(rank);
35+
} catch (e) {
4036
return e.message;
4137
}
4238
}
4339

44-
function isValidCard(rank,suit){
45-
const ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
40+
function isValidCard(rank, suit) {
41+
const ranks = [
42+
"A",
43+
"2",
44+
"3",
45+
"4",
46+
"5",
47+
"6",
48+
"7",
49+
"8",
50+
"9",
51+
"10",
52+
"J",
53+
"Q",
54+
"K",
55+
];
4656
const suits = ["♠", "♥", "♦", "♣"];
47-
if(ranks.includes(rank) && suits.includes(suit))
48-
return true;
49-
else
50-
return false;
57+
if (ranks.includes(rank) && suits.includes(suit)) return true;
58+
else return false;
5159
}
5260

5361
// The line below allows us to load the getCardValue function into tests in other files.
@@ -65,7 +73,6 @@ function assertEquals(actualOutput, targetOutput) {
6573
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
6674
// Examples:
6775
assertEquals(getCardValue("9♠"), 9);
68-
6976
assertEquals(getCardValue("10♠"), 10);
7077
assertEquals(getCardValue("Q♠"), 10);
7178
assertEquals(getCardValue("A♣"), 11);
@@ -76,4 +83,4 @@ assertEquals(getCardValue("Invalid"), "Invalid card");
7683
assertEquals(getCardValue("1Q"), "Invalid card");
7784
assertEquals(getCardValue("-10♦"), "Invalid card");
7885
assertEquals(getCardValue("♦K"), "Invalid card");
79-
assertEquals(getCardValue("Q♦♦"), "Invalid card");
86+
assertEquals(getCardValue("Q♦♦"), "Invalid card");

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,40 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
44

55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66

7-
// Special case: numerator is zero
7+
//Case 1: denominator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
10+
expect(isProperFraction(-1, 0)).toEqual(false);
11+
expect(isProperFraction(0, 0)).toEqual(false);
12+
expect(isProperFraction(3848473, 0)).toEqual(false);
13+
});
14+
15+
//Case 2: numerator is 0 but denominator is not 0
16+
test(`should return true when numerator is zero but denominator is not 0`, () => {
1017
expect(isProperFraction(0, 1)).toEqual(true);
11-
expect(isProperFraction(0, -1)).toEqual(true);
18+
expect(isProperFraction(0, -100)).toEqual(true);
19+
expect(isProperFraction(0, 1000)).toEqual(true);
20+
});
21+
22+
//Case 3: Absolute values of numerator and denominator are equal
23+
test(`should return false when numerator and denominator have equal absolute values`, () => {
1224
expect(isProperFraction(1, 1)).toEqual(false);
25+
expect(isProperFraction(-181, 181)).toEqual(false);
26+
expect(isProperFraction(839984, 839984)).toEqual(false);
27+
expect(isProperFraction(-1211, -1211)).toEqual(false);
28+
});
29+
30+
//Case 4: Absolute value of numerator is less than absolute value of denominator
31+
test(`should return true when absolute numerator is less than absolute denominator`, () => {
1332
expect(isProperFraction(1, 2)).toEqual(true);
1433
expect(isProperFraction(1, -2)).toEqual(true);
15-
expect(isProperFraction(-6, -2)).toEqual(false);
1634
expect(isProperFraction(-2, -6)).toEqual(true);
17-
expect(isProperFraction(-200, -0)).toEqual(false);
1835
expect(isProperFraction(-0, -1000)).toEqual(true);
36+
});
37+
38+
//Case 5: Absolute value of numerator is greater than absolute value of denominator
39+
test(`should return false when absolute numerator is greater than absolute denominator`, () => {
40+
expect(isProperFraction(-6, -2)).toEqual(false);
1941
expect(isProperFraction(5, 3)).toEqual(false);
2042
expect(isProperFraction(-25, 3)).toEqual(false);
2143
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test(`Should return 11 when given an ace card`, () => {
1010
});
1111

1212
// Case 2: Number Cards (2-10)
13-
test(`Should return number when given a number`, () => {
13+
test(`Should return number when given a number card`, () => {
1414
expect(getCardValue("2♠")).toEqual(2);
1515
expect(getCardValue("10♠")).toEqual(10);
1616
expect(getCardValue("5♠")).toEqual(5);

0 commit comments

Comments
 (0)