Skip to content

Commit aea8177

Browse files
committed
Complete implement and rewrite tests
1 parent 29ef74a commit aea8177

6 files changed

Lines changed: 253 additions & 18 deletions

File tree

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Implement a function getAngleType
22
//
3-
// Don't forget to write tests in implement-tests-with-node-test.
4-
//
53
// When given an angle in degrees, it should return a string indicating the type of angle:
64
// - "Acute angle" for angles greater than 0° and less than 90°
75
// - "Right angle" for exactly 90°
@@ -16,6 +14,54 @@
1614
// After you have implemented the function, write tests to cover all the cases, and
1715
// execute the code to ensure all tests pass.
1816

19-
export function getAngleType(angle) {
20-
// TODO: Implement this function
17+
function getAngleType(angle) {
18+
if (angle > 0 && angle < 90) {
19+
return `Acute angle`;
20+
} else if (angle === 90) {
21+
return `Right angle`;
22+
} else if (angle > 90 && angle < 180) {
23+
return `Obtuse angle`;
24+
} else if (angle === 180) {
25+
return `Straight angle`;
26+
} else if (angle > 180 && angle < 360) {
27+
return `Reflex angle`;
28+
} else {
29+
return `Invalid angle`;
30+
}
31+
}
32+
33+
// The line below allows us to load the getAngleType function into tests in other files.
34+
// This will be useful in the "rewrite tests with jest" step.
35+
module.exports = getAngleType;
36+
37+
// This helper function is written to make our assertions easier to read.
38+
// If the actual output matches the target output, the test will pass
39+
function assertEquals(actualOutput, targetOutput) {
40+
console.assert(
41+
actualOutput === targetOutput,
42+
`Expected ${actualOutput} to equal ${targetOutput}`
43+
);
2144
}
45+
46+
// TODO: Write tests to cover all cases, including boundary and invalid cases.
47+
// Example: Identify Right Angles
48+
const right = getAngleType(90);
49+
assertEquals(right, "Right angle");
50+
51+
const acute = getAngleType(78);
52+
assertEquals(acute, "Acute angle");
53+
54+
const obtuse = getAngleType(102);
55+
assertEquals(obtuse, "Obtuse angle");
56+
57+
const straight = getAngleType(180);
58+
assertEquals(straight, "Straight angle");
59+
60+
const reflex = getAngleType(250);
61+
assertEquals(reflex, "Reflex angle");
62+
63+
const invalid = getAngleType(420);
64+
assertEquals(invalid, "Invalid angle");
65+
66+
const zero = getAngleType(0);
67+
assertEquals(zero, "Invalid angle");
Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// Implement a function isProperFraction,
2-
//
3-
// Don't forget to write tests in implement-tests-with-node-test.
4-
//
5-
// When given two numbers, a numerator and a denominator, it should return true if
2+
// when given two numbers, a numerator and a denominator, it should return true if
63
// the given numbers form a proper fraction, and false otherwise.
74

85
// Assumption: The parameters are valid numbers (not NaN or Infinity).
@@ -13,6 +10,35 @@
1310
// After you have implemented the function, write tests to cover all the cases, and
1411
// execute the code to ensure all tests pass.
1512

16-
export function isProperFraction(numerator, denominator) {
17-
// TODO: Implement this function
13+
function isProperFraction(numerator, denominator) {
14+
// Non-unit fractions have a numerator that is more than 1, but less than the denominator.
15+
//Unit fractions all have a numerator of 1.
16+
17+
let validFraction =
18+
Math.abs(numerator) < Math.abs(denominator) && Math.abs(numerator) > 0;
19+
return validFraction;
20+
}
21+
22+
// The line below allows us to load the isProperFraction function into tests in other files.
23+
// This will be useful in the "rewrite tests with jest" step.
24+
module.exports = isProperFraction;
25+
26+
// Here's our helper again
27+
function assertEquals(actualOutput, targetOutput) {
28+
console.assert(
29+
actualOutput === targetOutput,
30+
`Expected ${actualOutput} to equal ${targetOutput}`
31+
);
1832
}
33+
34+
// TODO: Write tests to cover all cases.
35+
// What combinations of numerators and denominators should you test?
36+
37+
// Example: 1/2 is a proper fraction
38+
assertEquals(isProperFraction(1, 2), true);
39+
assertEquals(isProperFraction(1, 5), true);
40+
assertEquals(isProperFraction(0, 4), false);
41+
assertEquals(isProperFraction(5, 5), false);
42+
assertEquals(isProperFraction(6, 5), false);
43+
assertEquals(isProperFraction(3, 5), true);
44+
assertEquals(isProperFraction(1, 0), false);

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

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
22

3-
// Don't forget to write tests in implement-tests-with-node-test.
4-
53
// Implement a function getCardValue, when given a string representing a playing card,
64
// should return the numerical value of the card.
75

@@ -23,6 +21,82 @@
2321
// After you have implemented the function, write tests to cover all the cases, and
2422
// execute the code to ensure all tests pass.
2523

26-
export function getCardValue(card) {
27-
// TODO: Implement this function
24+
function getCardValue(card) {
25+
let regex = /^([2-9]|10|[AJQK])[♠♥♣♦]$/;
26+
const valid = regex.test(card);
27+
if (!valid) {
28+
throw new Error("Invalid card");
29+
}
30+
let rank = card.slice(0, -1);
31+
32+
if (rank === "A") {
33+
return 11;
34+
} else if (rank === "J" || rank === "Q" || rank === "K") {
35+
return 10;
36+
} else {
37+
rank = Number(rank);
38+
return rank;
39+
}
40+
}
41+
42+
// The line below allows us to load the getCardValue function into tests in other files.
43+
// This will be useful in the "rewrite tests with jest" step.
44+
module.exports = getCardValue;
45+
46+
// Helper functions to make our assertions easier to read.
47+
function assertEquals(actualOutput, targetOutput) {
48+
console.assert(
49+
actualOutput === targetOutput,
50+
`Expected ${actualOutput} to equal ${targetOutput}`
51+
);
52+
}
53+
54+
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
55+
// Examples:
56+
assertEquals(getCardValue("9♠"), 9);
57+
assertEquals(getCardValue("A♠"), 11);
58+
assertEquals(getCardValue("10♥"), 10);
59+
assertEquals(getCardValue("2♥"), 2);
60+
assertEquals(getCardValue("K♠"), 10);
61+
assertEquals(getCardValue("J♣"), 10);
62+
assertEquals(getCardValue("Q♦"), 10);
63+
64+
// Handling invalid cards
65+
try {
66+
getCardValue("invalid");
67+
68+
// This line will not be reached if an error is thrown as expected
69+
console.error("Error was not thrown for invalid card 😢");
70+
} catch (e) {
71+
console.log("Error thrown for invalid card 🎉");
72+
}
73+
74+
// What other invalid card cases can you think of?
75+
76+
try {
77+
getCardValue(3);
78+
console.error("Error was not thrown for number 3");
79+
} catch (e) {
80+
console.log("Error thrown for invalid format");
81+
}
82+
83+
try {
84+
getCardValue("11K♠");
85+
console.error("Error format too long");
86+
} catch (e) {
87+
console.log("Error thrown for too many characters");
88+
}
89+
90+
try {
91+
getCardValue(-1);
92+
console.error("Error for negative number");
93+
} catch (e) {
94+
console.log("Error thrown for negative number");
95+
}
96+
97+
try {
98+
getCardValue("22♥");
99+
console.error("Error for string 22♥");
100+
} catch (e) {
101+
console.log("Error thrown for string 22♥");
28102
}

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { getAngleType } from "../implement/1-get-angle-type.js";
1+
// This statement loads the getAngleType function you wrote in the implement directory.
2+
// We will use the same function, but write tests for it using Jest in this file.
3+
const getAngleType = require("../implement/1-get-angle-type");
24

35
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
46
// including boundary and invalid cases.
@@ -8,11 +10,37 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
810
// Test various acute angles, including boundary cases
911
expect(getAngleType(1)).toEqual("Acute angle");
1012
expect(getAngleType(45)).toEqual("Acute angle");
13+
expect(getAngleType(77)).toEqual("Acute angle");
14+
expect(getAngleType(24)).toEqual("Acute angle");
1115
expect(getAngleType(89)).toEqual("Acute angle");
1216
});
1317

1418
// Case 2: Right angle
19+
test(`should return "Right angle" when(angle === 90)`, () => {
20+
expect(getAngleType(90)).toEqual("Right angle");
21+
});
1522
// Case 3: Obtuse angles
23+
test(`should return "Obtuse angle" when (angle > 90 && angle <180)`, () => {
24+
expect(getAngleType(91)).toEqual("Obtuse angle");
25+
expect(getAngleType(133)).toEqual("Obtuse angle");
26+
expect(getAngleType(111)).toEqual("Obtuse angle");
27+
expect(getAngleType(179)).toEqual("Obtuse angle");
28+
});
1629
// Case 4: Straight angle
30+
test(`should return "Straight angle" when (angle ===180)`, () => {
31+
expect(getAngleType(180)).toEqual("Straight angle");
32+
});
1733
// Case 5: Reflex angles
34+
test(`should return "Reflex angle" when (angle > 180 && angle < 360)`, () => {
35+
expect(getAngleType(181)).toEqual("Reflex angle");
36+
expect(getAngleType(359)).toEqual("Reflex angle");
37+
expect(getAngleType(199)).toEqual("Reflex angle");
38+
expect(getAngleType(244)).toEqual("Reflex angle");
39+
});
1840
// Case 6: Invalid angles
41+
test(`should return "Invalid angle"`, () => {
42+
expect(getAngleType(361)).toEqual("Invalid angle");
43+
expect(getAngleType(2332)).toEqual("Invalid angle");
44+
expect(getAngleType(-1)).toEqual("Invalid angle");
45+
expect(getAngleType("-222r")).toEqual("Invalid angle");
46+
});
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
1-
import { isProperFraction } from "../implement/2-is-proper-fraction.js";
1+
// This statement loads the isProperFraction function you wrote in the implement directory.
2+
// We will use the same function, but write tests for it using Jest in this file.
3+
const isProperFraction = require("../implement/2-is-proper-fraction");
24

35
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
46

57
// Special case: numerator is zero
8+
69
test(`should return false when denominator is zero`, () => {
710
expect(isProperFraction(1, 0)).toEqual(false);
811
});
12+
test(`should return true when input (1, 2)`, () => {
13+
expect(isProperFraction(1, 2)).toEqual(true);
14+
});
15+
test(`should return false when (numerator = 0)`, () => {
16+
expect(isProperFraction(0, 4)).toEqual(false);
17+
});
18+
test(`should return false when (numerator===denominator)`, () => {
19+
expect(isProperFraction(5, 5)).toEqual(false);
20+
});
21+
test(`should return false when (numerator>denominator)`, () => {
22+
expect(isProperFraction(6, 5)).toEqual(false);
23+
});
24+
test(`should return false when (numerator ===0 && denominator === 0)`, () => {
25+
expect(isProperFraction(0, 0)).toEqual(false);
26+
});
27+
test(`should return true when numerator is negative number`, () => {
28+
expect(isProperFraction(-4, 8)).toEqual(true);
29+
});
30+
test(`should return false when numerator and denominator have equal absolute values `, () => {
31+
expect(isProperFraction(-5, 5)).toEqual(false);
32+
});
33+
test(`should return true when denominator is negative `, () => {
34+
expect(isProperFraction(3, -5)).toEqual(true);
35+
});
36+
test(`should return false when numerator is zero and denominator is negative zero`, () => {
37+
expect(isProperFraction(0, -0)).toEqual(false);
38+
});
39+
test(`should return true when numerator and denominator are negative`, () => {
40+
expect(isProperFraction(-3, -5)).toEqual(true);
41+
});

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { getCardValue } from "../implement/3-get-card-value.js";
1+
// This statement loads the getCardValue function you wrote in the implement directory.
2+
// We will use the same function, but write tests for it using Jest in this file.
3+
const getCardValue = require("../implement/3-get-card-value");
24

35
// TODO: Write tests in Jest syntax to cover all possible outcomes.
46

@@ -9,10 +11,36 @@ test(`Should return 11 when given an ace card`, () => {
911

1012
// Suggestion: Group the remaining test data into these categories:
1113
// Number Cards (2-10)
14+
let suits = ["♠", "♥", "♣", "♦"];
15+
for (let number = 2; number <= 10; number++) {
16+
for (let suit of suits) {
17+
test(`should return card value as number`, () => {
18+
expect(getCardValue(`${number}${suit}`)).toEqual(number);
19+
});
20+
}
21+
}
1222
// Face Cards (J, Q, K)
23+
24+
let chars = ["J", "Q", "K"];
25+
26+
for (let char of chars) {
27+
for (let suit of suits) {
28+
test(`should return card value as 10`, () => {
29+
expect(getCardValue(`${char}${suit}`)).toEqual(10);
30+
});
31+
}
32+
}
1333
// Invalid Cards
1434

35+
let invalidCards = ["22♥", "11K♠", -1, 3, "11^♠"];
36+
for (let card of invalidCards) {
37+
test(`should return "Invalid card"`, () => {
38+
expect(() => {
39+
getCardValue(card);
40+
}).toThrow();
41+
});
42+
}
43+
1544
// To learn how to test whether a function throws an error as expected in Jest,
1645
// please refer to the Jest documentation:
1746
// https://jestjs.io/docs/expect#tothrowerror
18-

0 commit comments

Comments
 (0)