Skip to content

Commit 5a9baf7

Browse files
author
lintsang
committed
finish 1-implement-and-rewrite-tests exercise
1 parent b31a586 commit 5a9baf7

6 files changed

Lines changed: 160 additions & 4 deletions

File tree

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
1515
// execute the code to ensure all tests pass.
1616

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

2133
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +47,18 @@ function assertEquals(actualOutput, targetOutput) {
3547
// Example: Identify Right Angles
3648
const right = getAngleType(90);
3749
assertEquals(right, "Right angle");
50+
51+
const acute = getAngleType(15);
52+
assertEquals(acute, "Acute angle");
53+
54+
const obtuse= getAngleType(115);
55+
assertEquals(obtuse, "Obtuse angle");
56+
57+
const straight = getAngleType(180);
58+
assertEquals(straight, "Straight line");
59+
60+
const reflexAngle = getAngleType(215);
61+
assertEquals(reflexAngle, "Reflex angle");
62+
63+
const invalid = getAngleType(380);
64+
assertEquals(invalid, "Invalid");

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

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

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
if (numerator < denominator){
15+
return true;
16+
}else{
17+
return false;
18+
}
1519
}
1620

1721
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +35,8 @@ function assertEquals(actualOutput, targetOutput) {
3135

3236
// Example: 1/2 is a proper fraction
3337
assertEquals(isProperFraction(1, 2), true);
38+
assertEquals(isProperFraction(2, 3), true);
39+
assertEquals(isProperFraction(499, 500), true);
40+
assertEquals(isProperFraction(5, 2), false);
41+
assertEquals(isProperFraction(5, 5), false);
42+
assertEquals(isProperFraction(1, 0), false);

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const number = card.slice(0, -1);
26+
const suit = card.slice(-1);
27+
//console.log(`number is ${number}`);
28+
//console.log(typeof suit);
29+
//console.log(suit);
30+
if (suit == "♠" || suit == "♥" || suit == "♦" || suit == "♣"){
31+
switch (number) {
32+
case "A":
33+
return 11;
34+
case "J":
35+
case "Q":
36+
case "K":
37+
return 10;
38+
case "2":
39+
case "3":
40+
case "4":
41+
case "5":
42+
case "6":
43+
case "7":
44+
case "8":
45+
case "9":
46+
return Number(number);
47+
default:
48+
return "invalid";
49+
50+
}
51+
}else{
52+
return "invalid";
53+
}
2654
}
2755

2856
// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,7 +67,19 @@ function assertEquals(actualOutput, targetOutput) {
3967

4068
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4169
// Examples:
42-
assertEquals(getCardValue("9♠"), 9);
70+
assertEquals(getCardValue("A♠"), 11);
71+
assertEquals(getCardValue("A♥"), 11);
72+
assertEquals(getCardValue("J♦"), 10);
73+
assertEquals(getCardValue("Q♦"), 10);
74+
assertEquals(getCardValue("K♦"), 10);
75+
assertEquals(getCardValue("2♣"), 2);
76+
assertEquals(getCardValue("3♣"), 3);
77+
assertEquals(getCardValue("4♦"), 4);
78+
assertEquals(getCardValue("5♦"), 5);
79+
assertEquals(getCardValue("6♦"), 6);
80+
assertEquals(getCardValue("7♦"), 7);
81+
assertEquals(getCardValue("8♣"), 8);
82+
assertEquals(getCardValue("9♣"), 9);
4383

4484
// Handling invalid cards
4585
try {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,37 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1414
});
1515

1616
// Case 2: Right angle
17+
test(`should return "Right angle" when (angle = 90)`, () => {
18+
// Test right angles, only one case
19+
expect(getAngleType(90)).toEqual("Right angle");
20+
});
21+
1722
// Case 3: Obtuse angles
23+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
24+
// Test various obtuse angles, including boundary cases
25+
expect(getAngleType(91)).toEqual("Obtuse angle");
26+
expect(getAngleType(125)).toEqual("Obtuse angle");
27+
expect(getAngleType(179)).toEqual("Obtuse angle");
28+
});
29+
1830
// Case 4: Straight angle
31+
test(`should return "Straight angle angle" when (angle = 180)`, () => {
32+
// Test right angles, only one case
33+
expect(getAngleType(180)).toEqual("Straight line");
34+
});
35+
1936
// Case 5: Reflex angles
37+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
38+
// Test various reflex angles, including boundary cases
39+
expect(getAngleType(181)).toEqual("Reflex angle");
40+
expect(getAngleType(225)).toEqual("Reflex angle");
41+
expect(getAngleType(359)).toEqual("Reflex angle");
42+
});
43+
2044
// Case 6: Invalid angles
45+
test(`should return "Invalid" when ( angle < 0 or angle > 360 )`, () => {
46+
// Test various invalid angle, including boundary cases
47+
expect(getAngleType(-1)).toEqual("Invalid");
48+
expect(getAngleType(361)).toEqual("Invalid");
49+
expect(getAngleType(550)).toEqual("Invalid");
50+
});

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
12+
// Proper fraction cases
13+
test(`should return true when numerator is smaller then denominator`, () => {
14+
expect(isProperFraction(1, 2)).toEqual(true);
15+
expect(isProperFraction(65, 67)).toEqual(true);
16+
expect(isProperFraction(1, 1000)).toEqual(true);
17+
expect(isProperFraction(999, 1000)).toEqual(true);
18+
expect(isProperFraction(0, 6)).toEqual(true);
19+
});
20+
21+
// Improper fraction cases
22+
test(`should return false when numerator is bigger then denominator`, () => {
23+
expect(isProperFraction(3, 2)).toEqual(false);
24+
expect(isProperFraction(65, 60)).toEqual(false);
25+
expect(isProperFraction(1000, 1)).toEqual(false);
26+
expect(isProperFraction(2999, 1000)).toEqual(false);
27+
expect(isProperFraction(11, 10)).toEqual(false);
28+
});

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ const getCardValue = require("../implement/3-get-card-value");
77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
10+
expect(getCardValue("A♦")).toEqual(11);
11+
expect(getCardValue("A♠")).toEqual(11);
12+
});
13+
14+
// Case 2: Face cards Jack, Queen, King (J,Q,K)
15+
test(`Should return 10 when given a face card`, () => {
16+
expect(getCardValue("J♠")).toEqual(10);
17+
expect(getCardValue("J♦")).toEqual(10);
18+
expect(getCardValue("Q♣")).toEqual(10);
19+
expect(getCardValue("K♥")).toEqual(10);
20+
});
21+
22+
// Case 3: Number cards 2-10 (2,3,4,5,6,7,8,9)
23+
test(`Should return the card value when given a number card`, () => {
24+
expect(getCardValue("2♠")).toEqual(2);
25+
expect(getCardValue("3♦")).toEqual(3);
26+
expect(getCardValue("4♣")).toEqual(4);
27+
expect(getCardValue("5♥")).toEqual(5);
28+
expect(getCardValue("6♠")).toEqual(6);
29+
expect(getCardValue("7♦")).toEqual(7);
30+
expect(getCardValue("8♣")).toEqual(8);
31+
expect(getCardValue("9♥")).toEqual(9);
32+
});
33+
34+
// Case 4: Invalid card
35+
test(`Should return 10 when given an ace card`, () => {
36+
expect(getCardValue("invalid")).toEqual("invalid");
37+
expect(getCardValue("♦")).toEqual("invalid");
38+
expect(getCardValue("4")).toEqual("invalid");
39+
expect(getCardValue("")).toEqual("invalid");
40+
expect(getCardValue("m")).toEqual("invalid");
41+
expect(getCardValue("300")).toEqual("invalid");
1042
});
1143

1244
// Suggestion: Group the remaining test data into these categories:

0 commit comments

Comments
 (0)