Skip to content

Commit c412519

Browse files
implement and rewrite tests
1 parent bab774c commit c412519

3 files changed

Lines changed: 117 additions & 4 deletions

File tree

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@
1616

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

2139
// The line below allows us to load the getAngleType function into tests in other files.
@@ -32,6 +50,31 @@ function assertEquals(actualOutput, targetOutput) {
3250
}
3351

3452
// TODO: Write tests to cover all cases, including boundary and invalid cases.
35-
// Example: Identify Right Angles
36-
const right = getAngleType(90);
37-
assertEquals(right, "Right angle");
53+
54+
// Tests for right angles
55+
assertEquals(getAngleType(90), "Right angle");
56+
57+
// Tests for acute angles
58+
assertEquals(getAngleType(56), "Acute angle");
59+
assertEquals(getAngleType(1), "Acute angle");
60+
61+
// Test for a straight line
62+
assertEquals(getAngleType(180), "Straight angle");
63+
64+
// Test for obtuse angle
65+
assertEquals(getAngleType(95), "Obtuse angle");
66+
assertEquals(getAngleType(160), "Obtuse angle");
67+
assertEquals(getAngleType(102), "Obtuse angle");
68+
69+
// Test for reflex angle
70+
assertEquals(getAngleType(181), "Reflex angle");
71+
assertEquals(getAngleType(249), "Reflex angle");
72+
73+
// Test for invalid angles
74+
assertEquals(getAngleType(0), "Invalid angle");
75+
assertEquals(getAngleType(361), "Invalid angle");
76+
assertEquals(getAngleType(-980), "Invalid angle");
77+
assertEquals(getAngleType(90), "Right angle");
78+
assertEquals(getAngleType(672), "Invalid angle");
79+
console.log("Execution finished! If any test failed, console.assert errors will appear above.");
80+

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

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

1313
function isProperFraction(numerator, denominator) {
14+
return numerator < denominator;
1415
// TODO: Implement this function
1516
}
1617

@@ -31,3 +32,7 @@ function assertEquals(actualOutput, targetOutput) {
3132

3233
// Example: 1/2 is a proper fraction
3334
assertEquals(isProperFraction(1, 2), true);
35+
assertEquals(isProperFraction(93, 112), true);
36+
assertEquals(isProperFraction(2, 1), false);
37+
assertEquals(isProperFraction(80, 80), false);
38+
console.log("Execution finished! If any test failed, console.assert errors will appear above.");

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,39 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
const ranks = [
27+
"A",
28+
"2",
29+
"3",
30+
"4",
31+
"5",
32+
"6",
33+
"7",
34+
"8",
35+
"9",
36+
"10",
37+
"J",
38+
"Q",
39+
"K",
40+
];
41+
const suits = ["♠", "♥", "♦", "♣"];
42+
let rank = card.slice(0, -1)// This is extracting the bit before the suits alone
43+
let suit = card.slice(-1) // THis is extracting the suit in the string
44+
if(!ranks.includes(rank)){
45+
throw new Error("Invalid rank")
46+
}
47+
if (!suits.includes(suit)){
48+
throw new Error("Invalid suit")
49+
}
50+
if (rank === "A"){
51+
return 11}
52+
if (["J", "K", "Q"].includes(rank)){
53+
return 10
54+
}
55+
else{
56+
return Number(rank)
57+
}
2658
}
27-
2859
// The line below allows us to load the getCardValue function into tests in other files.
2960
// This will be useful in the "rewrite tests with jest" step.
3061
module.exports = getCardValue;
@@ -40,6 +71,20 @@ function assertEquals(actualOutput, targetOutput) {
4071
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4172
// Examples:
4273
assertEquals(getCardValue("9♠"), 9);
74+
assertEquals(getCardValue("A♠"), 11);
75+
assertEquals(getCardValue("J♣"), 10);
76+
assertEquals(getCardValue("K♦"), 10);
77+
assertEquals(getCardValue("5♣"), 5);
78+
assertEquals(getCardValue("2♣"), 2);
79+
assertEquals(getCardValue("10♥"), 10);
80+
assertEquals(getCardValue("6♥"), 6);
81+
assertEquals(getCardValue("5♥"), 5);
82+
assertEquals(getCardValue("8♥"), 8);
83+
assertEquals(getCardValue("4♥"), 4);
84+
assertEquals(getCardValue("3♥"), 3);
85+
assertEquals(getCardValue("Q♦"), 10);
86+
assertEquals(getCardValue("7♥"), 7);
87+
4388

4489
// Handling invalid cards
4590
try {
@@ -52,3 +97,23 @@ try {
5297
}
5398

5499
// What other invalid card cases can you think of?
100+
101+
try {
102+
getCardValue("A");
103+
console.error("Error not thrown for missing suit");
104+
} catch (e) {
105+
console.log("Error thrown for missing suit 🎉");
106+
}
107+
108+
try {
109+
getCardValue("");
110+
console.error("Error not thrown for empty string");
111+
} catch (e) {
112+
console.log("Error thrown for empty string🎉");
113+
}
114+
try {
115+
getCardValue("Apple");
116+
console.error("Error not thrown");
117+
} catch {
118+
console.log("Error thrown 🎉");
119+
}

0 commit comments

Comments
 (0)