Skip to content

Commit 9eed6df

Browse files
completed the implement exercise
1 parent 3372770 commit 9eed6df

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616

1717
function getAngleType(angle) {
1818
// TODO: Implement this function
19+
if (angle > 0 && angle < 90) {
20+
return "Acute angle";
21+
} else if (angle === 90) {
22+
return "Right angle";
23+
} else if (angle > 90 && angle < 180) {
24+
return "Obtuse angle";
25+
} else if (angle === 180) {
26+
return "Straight angle";
27+
} else if (angle > 180 && angle < 360) {
28+
return "Reflex angle";
29+
} else {
30+
return "Invalid angle";
31+
}
1932
}
2033

2134
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +48,24 @@ function assertEquals(actualOutput, targetOutput) {
3548
// Example: Identify Right Angles
3649
const right = getAngleType(90);
3750
assertEquals(right, "Right angle");
51+
52+
const Acute = getAngleType(45);
53+
assertEquals(Acute, "Acute angle")
54+
55+
const Obtuse = getAngleType(120);
56+
assertEquals(Obtuse, "Obtuse angle")
57+
58+
const Straight = getAngleType(180);
59+
assertEquals(Straight, "Straight angle")
60+
61+
const Reflex = getAngleType(270);
62+
assertEquals(Reflex, "Reflex angle")
63+
64+
const InvalidNegative= getAngleType(-10);
65+
assertEquals(InvalidNegative, "Invalid angle")
66+
67+
const InvalidOver= getAngleType(400);
68+
assertEquals(InvalidOver, "Invalid angle")
69+
70+
const InvalidZero= getAngleType(0);
71+
assertEquals(InvalidZero, "Invalid angle")

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14+
if (denominator >numerator && denominator !==0) {
15+
return ture ;
16+
17+
}
18+
else{
19+
return false;
20+
}
21+
return Math.abs(numerator) < Math.abs(denominator);
1422
// TODO: Implement this function
1523
}
1624

@@ -31,3 +39,16 @@ function assertEquals(actualOutput, targetOutput) {
3139

3240
// Example: 1/2 is a proper fraction
3341
assertEquals(isProperFraction(1, 2), true);
42+
43+
//2/1 is not a proper fraction
44+
assertEquals(isProperFraction(2,1), false);
45+
//0/1 is a proper fraction
46+
assertEquals(isProperFraction(0,1), true);
47+
//negative fraction -1/2 is a proper fraction
48+
assertEquals(isProperFraction(-1,2), true);
49+
//negative fraction -1/-2 is not a proper fraction
50+
assertEquals(isProperFraction(-1,-2), false);
51+
//fraction with zero denominator is not a proper fraction
52+
assertEquals(isProperFraction(1,0), false);
53+
//fraction with zero numerator is a proper fraction
54+
assertEquals(isProperFraction(0,5), true);

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

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,27 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
let rank = card.slice(0,-1);
27+
let cardFace = card[card.length - 1];
28+
29+
if (!["♠", "♥", "♦", "♣"].includes(cardFace)) {
30+
throw new Error(`Invalid card face: ${cardFace}`);
31+
}
32+
if (rank === "A") {
33+
return 11;
34+
}
35+
if (+rank >= 2 && +rank <= 9) {
36+
return +rank;
37+
}
38+
if (["K", "10", "Q", "J"].includes(rank)) {
39+
return 10;
40+
}
41+
if (!["♠", "♥", "♦", "♣"].includes(cardFace)) {
42+
}
43+
throw new Error(`Invalid card rank: ${rank}`);
2644
}
2745

46+
2847
// The line below allows us to load the getCardValue function into tests in other files.
2948
// This will be useful in the "rewrite tests with jest" step.
3049
module.exports = getCardValue;
@@ -39,14 +58,61 @@ function assertEquals(actualOutput, targetOutput) {
3958

4059
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4160
// Examples:
42-
assertEquals(getCardValue("9♠"), 9);
61+
// (Ace All suits)
62+
const aceOfSpades = getCardValue("A♠");
63+
assertEquals(aceOfSpades, 11);
64+
65+
const aceOfHearts = getCardValue("A♥");
66+
assertEquals(aceOfHearts, 11);
67+
68+
const aceOfDiamonds = getCardValue("A♦");
69+
assertEquals(aceOfDiamonds, 11);
70+
71+
const aceOfClubs = getCardValue("A♣");
72+
assertEquals(aceOfClubs, 11);
73+
74+
// (Face cards)
75+
const jackOfHearts = getCardValue("J♥");
76+
assertEquals(jackOfHearts, 10);
77+
78+
const queenOfClubs = getCardValue("Q♣");
79+
assertEquals(queenOfClubs, 10);
80+
81+
const kingOfSpades = getCardValue("K♠");
82+
assertEquals(kingOfSpades, 10);
83+
84+
// (Number cards)
85+
const threeOfDiamonds = getCardValue("3♦");
86+
assertEquals(threeOfDiamonds, 3);
87+
88+
const sevenOfClubs = getCardValue("7♣");
89+
assertEquals(sevenOfClubs, 7);
90+
91+
4392

4493
// Handling invalid cards
94+
// giving an invalid rank (a number or an recognized face card)
95+
// When the function is called with such a card,
96+
// Then it should throw an error indicating "Invalid card rank."
4597
try {
4698
getCardValue("invalid");
4799

48100
// This line will not be reached if an error is thrown as expected
49101
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
102+
} catch (error) {}
51103

52104
// What other invalid card cases can you think of?
105+
106+
try {
107+
getCardValue("1♠");
108+
console.error("Error was not thrown for invalid card rank");
109+
} catch (error) {
110+
assertEquals(error.message, "Invalid card rank");
111+
}
112+
113+
try {
114+
getCardValue("5X");
115+
console.error("Error was not thrown for invalid card suit");
116+
} catch (error) {
117+
assertEquals(error.message, "Invalid card suit");
118+
}

0 commit comments

Comments
 (0)