Skip to content

Commit 8270fd6

Browse files
committed
refactored
1 parent 09fe227 commit 8270fd6

1 file changed

Lines changed: 38 additions & 37 deletions

File tree

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

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,35 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
const rank = card.slice(0, card.length -1).toUpperCase()
26-
const suit = card.slice(card.length -1)
27-
28-
const suits = ["♠", "♥", "♦", "♣"];
29-
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
30-
31-
// Basic validation
32-
if(card === ""){
25+
// Basic validation: validating the input before slicing
26+
if (card === "") {
3327
throw new Error("No card was played")
3428
}
35-
if(card.length < 2 || card.length > 3 ){
29+
if (card.length < 2 || card.length > 3) {
3630
throw new Error("Invalid card played, rank and suit cannot be less than 1 or more than 3")
3731
}
38-
// Suit validation
39-
if(!suits.includes(suit)){
32+
const rank = card.slice(0, card.length - 1).toUpperCase()
33+
const suit = card.slice(card.length - 1)
34+
35+
const validSuits = ["♠", "♥", "♦", "♣"];
36+
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
37+
38+
// Suit and rank validation
39+
if (!validSuits.includes(suit)) {
4040
throw new Error("Invalid card played, suit is missing");
4141
}
42-
// Rank validation
43-
if(!validRanks.includes(rank)){
44-
throw new Error("Invalid rank played")
45-
}
46-
if(rank === "A"){
47-
return 11
48-
}else if(rank === "J" || rank === "Q" || rank === "K"){
49-
return 10
50-
}else{
51-
return Number(rank)
42+
if (validRanks.includes(rank)) {
43+
if (rank === "A") {
44+
return 11;
45+
} else if (rank === "J" || rank === "Q" || rank === "K") {
46+
return 10;
47+
} else {
48+
return Number(rank);
49+
}
50+
} else {
51+
throw new Error("Invalid rank");
5252
}
53+
5354
}
5455

5556

@@ -59,10 +60,10 @@ module.exports = getCardValue;
5960

6061
// Helper functions to make our assertions easier to read.
6162
function assertEquals(actualOutput, targetOutput) {
62-
console.assert(
63-
actualOutput === targetOutput,
64-
`Expected ${actualOutput} to equal ${targetOutput}`
65-
);
63+
console.assert(
64+
actualOutput === targetOutput,
65+
`Expected ${actualOutput} to equal ${targetOutput}`
66+
);
6667
}
6768

6869
// Examples:
@@ -85,39 +86,39 @@ assertEquals(getCardValue("K♦"), 10);
8586

8687
// Handling invalid cards
8788
try {
88-
getCardValue("");
89+
getCardValue("");
8990

90-
// This line will not be reached if an error is thrown as expected
91-
console.error("Error was not thrown for invalid 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 😢");
9293
} catch (e) {
93-
console.log(e);
94+
console.log(e);
9495
}
9596

9697
// What other invalid card cases can you think of?
97-
try{
98+
try {
9899
getCardValue("100");
99100
console.error("Error was not thrown for card with more than 3 in length");
100-
} catch (e){
101+
} catch (e) {
101102
console.log(e)
102103
}
103-
try{
104+
try {
104105
getCardValue("1")
105106
console.error("Error was not thrown for a card.lenght = 1")
106-
}catch (e){
107+
} catch (e) {
107108
console.log(e)
108109
}
109110

110-
try{
111+
try {
111112
getCardValue("♦")
112113
console.error("Error was not thrown for a card play of just suits")
113-
}catch (e){
114+
} catch (e) {
114115
console.log(e)
115116
}
116117

117-
try{
118+
try {
118119
getCardValue("A😊")
119120
console.error("Error was not thrown for a card play of a wrong suit")
120-
}catch (e){
121+
} catch (e) {
121122
console.log(e)
122123
}
123124

0 commit comments

Comments
 (0)