Skip to content

Commit 95d6032

Browse files
made the code cleaner
1 parent 7e99eac commit 95d6032

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ function getCardValue(card) {
3737
// Extract rank (everything except last character)
3838
const rank = card.slice(0, -1);
3939

40-
// Validate suit
41-
if (!validSuits.includes(suit)) {
42-
throw new Error('Invalid card');
43-
}
44-
45-
// Validate rank
46-
if (!validRanks.includes(rank)) {
40+
// Validate suit and rank
41+
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
4742
throw new Error('Invalid card');
4843
}
4944

@@ -61,15 +56,17 @@ function getCardValue(card) {
6156
// This will be useful in the "rewrite tests with jest" step.
6257
module.exports = getCardValue;
6358

64-
// Helper functions to make our assertions easier to read.
65-
function assertEquals(actualOutput, targetOutput) {
66-
console.assert(
67-
actualOutput === targetOutput,
68-
`Expected ${actualOutput} to equal ${targetOutput}`
69-
);
70-
}
59+
// Only run tests if this file is executed directly (not imported as a module)
60+
if (require.main === module) {
61+
// Helper functions to make our assertions easier to read.
62+
function assertEquals(actualOutput, targetOutput) {
63+
console.assert(
64+
actualOutput === targetOutput,
65+
`Expected ${actualOutput} to equal ${targetOutput}`
66+
);
67+
}
7168

72-
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
69+
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
7370

7471
// Test all Ace cards (should return 11)
7572
assertEquals(getCardValue("A♠"), 11);
@@ -155,3 +152,5 @@ try {
155152
} catch (e) {}
156153

157154
console.log("All tests completed!");
155+
156+
} // End of: if (require.main === module)

0 commit comments

Comments
 (0)