Skip to content

Commit f960b94

Browse files
committed
Sprint 3: Implement functions and rewrite tests in Jest
1 parent 3372770 commit f960b94

File tree

3 files changed

+96
-14
lines changed

3 files changed

+96
-14
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
// - "Obtuse angle" for angles greater than 90° and less than 180°
77
// - "Straight angle" for exactly 180°
88
// - "Reflex angle" for angles greater than 180° and less than 360°
9-
// - "Invalid angle" for angles outside the valid range.
9+
// - "(angle >= 0 || angle <= 90){
10+
// Invalid angle" for angles outside the valid range.
1011

1112
// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)
1213

@@ -15,7 +16,17 @@
1516
// execute the code to ensure all tests pass.
1617

1718
function getAngleType(angle) {
18-
// 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 return "Invalid angle";
1930
}
2031

2132
// The line below allows us to load the getAngleType function into tests in other files.
@@ -33,5 +44,19 @@ function assertEquals(actualOutput, targetOutput) {
3344

3445
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3546
// Example: Identify Right Angles
47+
const acute = getAngleType(45)
48+
assertEquals(acute, "Acute angle");
3649
const right = getAngleType(90);
3750
assertEquals(right, "Right angle");
51+
const obtuse = getAngleType(110)
52+
assertEquals(obtuse, "Obtuse angle");
53+
const straight = getAngleType(180);
54+
assertEquals(straight, "Straight angle");
55+
const reflex = getAngleType(250);
56+
assertEquals(reflex, "Reflex angle");
57+
const invalid = getAngleType(380);
58+
assertEquals(invalid, "Invalid angle");
59+
const invalid2 = getAngleType(0);
60+
assertEquals(invalid2, "Invalid angle");
61+
const invalid3 = getAngleType(-10);
62+
assertEquals(invalid3, "Invalid angle");

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if(numerator <= 0 || denominator <= 0){
16+
return false;
17+
}else if(numerator < denominator){
18+
return true;
19+
}else return false;
1520
}
1621

1722
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -20,8 +25,7 @@ module.exports = isProperFraction;
2025

2126
// Here's our helper again
2227
function assertEquals(actualOutput, targetOutput) {
23-
console.assert(
24-
actualOutput === targetOutput,
28+
console.assert(actualOutput === targetOutput,
2529
`Expected ${actualOutput} to equal ${targetOutput}`
2630
);
2731
}
@@ -30,4 +34,10 @@ function assertEquals(actualOutput, targetOutput) {
3034
// What combinations of numerators and denominators should you test?
3135

3236
// Example: 1/2 is a proper fraction
33-
assertEquals(isProperFraction(1, 2), true);
37+
assertEquals(isProperFraction(1, 2), "true");
38+
assertEquals(isProperFraction(3, 2), "false");
39+
assertEquals(isProperFraction(1, 0), "false");
40+
assertEquals(isProperFraction(8, 9), "true");
41+
assertEquals(isProperFraction(0, 1), "false");
42+
assertEquals(isProperFraction(-4, 1), "false");
43+
assertEquals(isProperFraction(2, -4), "false");

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
26-
}
25+
if (typeof card !== "string") {
26+
throw new Error("Invalid card");
27+
}
28+
let rank = card.slice(0, -1); // Get everything except the last character
29+
let suit = card.slice(-1); // Get the last character
30+
31+
const validSuits = ["♠", "♥", "♦", "♣"]; // check if suit is valid
32+
if (!validSuits.includes(suit)) {
33+
throw new Error("Invalid card");
34+
}
2735

36+
if (rank === "A"){
37+
return 11;
38+
}else if(rank.match(/J|Q|K/)){
39+
return 10;
40+
}else if(rank.match(/^(10|[2-9])$/)){
41+
return Number(rank);
42+
}else throw new Error("Invalid card");
43+
}
2844
// The line below allows us to load the getCardValue function into tests in other files.
2945
// This will be useful in the "rewrite tests with jest" step.
3046
module.exports = getCardValue;
@@ -36,17 +52,48 @@ function assertEquals(actualOutput, targetOutput) {
3652
`Expected ${actualOutput} to equal ${targetOutput}`
3753
);
3854
}
39-
4055
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
4256
assertEquals(getCardValue("9♠"), 9);
57+
assertEquals(getCardValue("A♦"), 11);
58+
assertEquals(getCardValue("J♣"), 10);
59+
assertEquals(getCardValue("Q♥"), 10);
60+
assertEquals(getCardValue("K♠"), 10);
61+
assertEquals(getCardValue("3♦"), 3);
4362

44-
// Handling invalid cards
4563
try {
46-
getCardValue("invalid");
64+
getCardValue("J");
4765

48-
// This line will not be reached if an error is thrown as expected
66+
// The below line will not be reached if an error is thrown as expected
4967
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
51-
68+
} catch (e) {
69+
console.log('Test passed for "J": caught error ->', e.message);
70+
}
5271
// What other invalid card cases can you think of?
72+
73+
try {
74+
getCardValue("9X"); // invalid suit
75+
console.error('Test failed for "9X": error was not thrown');
76+
} catch (e) {
77+
console.log('Test passed for "9X": caught error ->', e.message);
78+
}
79+
80+
try {
81+
getCardValue("1♠"); // invalid rank
82+
console.error('Test failed for "1♠": error was not thrown');
83+
} catch (e) {
84+
console.log('Test passed for "1♠": caught error ->', e.message);
85+
}
86+
87+
try {
88+
getCardValue("0♥"); // invalid rank
89+
console.error('Test failed for "0♥": error was not thrown');
90+
} catch (e) {
91+
console.log('Test passed for "0♥": caught error ->', e.message);
92+
}
93+
94+
try {
95+
getCardValue("ABC"); // completely wrong format
96+
console.error('Test failed for "ABC": error was not thrown');
97+
} catch (e) {
98+
console.log('Test passed for "ABC": caught error ->', e.message);
99+
}

0 commit comments

Comments
 (0)