Skip to content

Commit ad96f8c

Browse files
author
Arthur
committed
Throw an error bug
1 parent b9af102 commit ad96f8c

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ function assertEquals(actualOutput, targetOutput) {
3131

3232
// Example: 1/2 is a proper fraction
3333
assertEquals(isProperFraction(1, 2), true);
34+
assertEquals(isProperFraction(2, 1), false);
35+
assertEquals(isProperFraction(2, 2), false);
36+
37+
38+
assertEquals(isProperFraction(0, 2), true);
39+
40+
41+
42+

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

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
2-
3-
// Implement a function getCardValue, when given a string representing a playing card,
1+
/// Implement a function getCardValue, when given a string representing a playing card,
42
// should return the numerical value of the card.
53

64
// A valid card string will contain a rank followed by the suit.
@@ -22,9 +20,25 @@
2220
// execute the code to ensure all tests pass.
2321

2422
function getCardValue(card) {
25-
// TODO: Implement this function
26-
}
23+
const rank = card.slice(0, -1);
24+
25+
26+
27+
if (rank === "A"){
28+
return 11 ;
29+
}
30+
else if (rank === "J" || rank === "Q" || rank === "K"){
31+
return 10 ;
32+
};
33+
2734

35+
const value = Number(rank);
36+
37+
if( value>=2 && value <= 10 ){
38+
return value};
39+
40+
throw new Error("invalid card!");
41+
}
2842
// The line below allows us to load the getCardValue function into tests in other files.
2943
// This will be useful in the "rewrite tests with jest" step.
3044
module.exports = getCardValue;
@@ -39,7 +53,7 @@ function assertEquals(actualOutput, targetOutput) {
3953

4054
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4155
// Examples:
42-
assertEquals(getCardValue("9"), 9);
56+
assertEquals(getCardValue("9"), 9);
4357

4458
// Handling invalid cards
4559
try {
@@ -51,4 +65,25 @@ try {
5165
console.log("Error thrown for invalid card 🎉");
5266
}
5367

68+
69+
try {
70+
getCardValue("null");
71+
72+
console.error("Error was not thrown for null card 😢");
73+
} catch (e) {
74+
console.log("Error thrown for invalid card 🎉")
75+
}
76+
77+
try {
78+
getCardValue("0");
79+
80+
console.error("Error was not thrown for 0 card 😢");
81+
} catch (e) {
82+
console.log("Error thrown for invalid card 🎉")
83+
}
5484
// What other invalid card cases can you think of?
85+
86+
87+
88+
89+

0 commit comments

Comments
 (0)