Skip to content

Commit 3b28213

Browse files
committed
Save updates to proper-fraction and card-value functions
1 parent 5b0a96e commit 3b28213

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@ console.log(isProperFraction(-4, 7));
6868
console.log(isProperFraction(-3, 3));
6969
console.log(isProperFraction(-2, -3)); // This is a proper fraction because the absolute value of numerator 2 and denominator is 3.
7070
// the value of numerator is less than denominator.
71+
console.log(isProperFraction(-4, -7));

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

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,60 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11-
const rank = card.slice(0, -1); // get the rank (before the suit symbol)
11+
if (typeof card !== "string") {
12+
throw new Error("Card must be a string")
13+
}
1214

15+
if (card.length < 2) {
16+
throw new Error("Invalid card rank")
17+
}
1318

14-
const num = Number(rank); // converting to a number.
19+
const rank = card.slice(0, -1);
20+
const suit = card.slice(-1);
1521

16-
if (!isNaN(rank)) {
17-
return num; // Number card
22+
if (!["♠", "♥", "♦", "♣"].includes(suit)) {
23+
throw new Error("Invalid card rank");
1824
}
1925

26+
if (rank === "A") return 11; // Ace
27+
28+
29+
30+
//const rank = card.slice(0, -1); // get the rank (before the suit symbol)
31+
32+
33+
// const num = Number(rank); // converting to a number.
34+
35+
// if (!isNaN(rank)) {
36+
// return num; // Number card
37+
// }
38+
2039
if (rank === "J" || rank === "Q" || rank === "K") {
2140
return 10; // Face cards
2241
}
2342

24-
if (rank === "A") return 11; // Ace
43+
if (!/^[0-9]+$/.test(rank)) {
44+
throw new Error("Invalid card rank");
45+
}
46+
// Reject weird or malformed ranks (like 3AAAA)
47+
48+
const num = Number(rank);
49+
// Convert numeric string
50+
51+
if (num >= 2 && num <= 10) {
52+
return num;
53+
}
54+
// Accept valid 2–10 range only
55+
56+
57+
2558

2659
// Anything else is invalid
2760
throw new Error("Invalid card rank")
2861

2962
}
3063

64+
3165
console.log(Number("0x002"));
3266
console.log(Number("2.1"));
3367
console.log(Number("9e1"));
@@ -62,31 +96,31 @@ function assertEquals(actualOutput, targetOutput) {
6296
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
6397
// When the function getCardValue is called with this card string as input,
6498
// Then it should return the numerical card value
65-
const aceofSpades = getCardValue("A♠");
66-
assertEquals(aceofSpades, 11);
99+
const aceOfSpades = getCardValue("A♠");
100+
assertEquals(aceOfSpades, 11);
67101

68102
// Handle Number Cards (2-10):
69103
// Given a card with a rank between "2" and "9",
70104
// When the function is called with such a card,
71105
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
72-
const fiveofHearts = getCardValue("5♥");
73-
assertEquals(fiveofHearts, 5);
106+
const fiveOfHearts = getCardValue("5♥");
107+
assertEquals(fiveOfHearts, 5);
74108

75-
const tenofDiamonds = getCardValue("10♦");
76-
assertEquals(tenofDiamonds, 10);
109+
const tenOfDiamonds = getCardValue("10♦");
110+
assertEquals(tenOfDiamonds, 10);
77111

78112
// Handle Face Cards (J, Q, K):
79113
// Given a card with a rank of "10," "J," "Q," or "K",
80114
// When the function is called with such a card,
81115
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
82-
const jackofClubs = getCardValue("J♣");
83-
assertEquals(jackofClubs, 10);
116+
const jackOfClubs = getCardValue("J♣");
117+
assertEquals(jackOfClubs, 10);
84118

85-
const queenofHearts = getCardValue("Q♥");
86-
assertEquals(queenofHearts, 10);
119+
const queenOfHearts = getCardValue("Q♥");
120+
assertEquals(queenOfHearts, 10);
87121

88-
const kingofSpades = getCardValue("K♠");
89-
assertEquals(kingofSpades, 10);
122+
const kingOfSpades = getCardValue("K♠");
123+
assertEquals(kingOfSpades, 10);
90124
// Handle Ace (A):
91125
// Given a card with a rank of "A",
92126
// When the function is called with an Ace,
@@ -96,8 +130,8 @@ assertEquals(kingofSpades, 10);
96130
// Given a card with an invalid rank (neither a number nor a recognized face card),
97131
// When the function is called with such a card,
98132
// Then it should throw an error indicating "Invalid card rank."
99-
// try {
100-
// getCardValue("Z♠");
101-
// } catch (error) {
102-
// console.log("Caught error:", error.message); // Should say "Invalid card rank"
103-
// }
133+
try {
134+
getCardValue("Z♠");
135+
} catch (error) {
136+
console.log("Caught error:", error.message); // Should say "Invalid card rank"
137+
}

0 commit comments

Comments
 (0)