Skip to content

Commit 46a0c2e

Browse files
committed
Complete Sprint 3 implement and rewrite exercises
1 parent 91ccb0c commit 46a0c2e

6 files changed

Lines changed: 156 additions & 4 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,32 @@ test("Classifies right angles", () => {
1010
const right = getAngleType(90);
1111
assert.equal(right, "Right angle");
1212
});
13+
14+
test("Classifies acute angles", () => {
15+
assert.equal(getAngleType(45), "Acute angle");
16+
assert.equal(getAngleType(1), "Acute angle");
17+
assert.equal(getAngleType(89), "Acute angle");
18+
});
19+
20+
test("Classifies obtuse angles", () => {
21+
assert.equal(getAngleType(91), "Obtuse angle");
22+
assert.equal(getAngleType(135), "Obtuse angle");
23+
assert.equal(getAngleType(179), "Obtuse angle");
24+
});
25+
26+
test("Classifies straight angles", () => {
27+
assert.equal(getAngleType(180), "Straight angle");
28+
});
29+
30+
test("Classifies reflex angles", () => {
31+
assert.equal(getAngleType(181), "Reflex angle");
32+
assert.equal(getAngleType(270), "Reflex angle");
33+
assert.equal(getAngleType(359), "Reflex angle");
34+
});
35+
36+
test("Classifies invalid angles", () => {
37+
assert.equal(getAngleType(0), "Invalid angle");
38+
assert.equal(getAngleType(360), "Invalid angle");
39+
assert.equal(getAngleType(400), "Invalid angle");
40+
assert.equal(getAngleType(-5), "Invalid angle");
41+
})

‎implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
77
// What combinations of numerators and denominators should you test?
88

99
test("Basic proper fraction", () => {
10-
// Example: 1/2 is a proper fraction
1110
assert.equal(isProperFraction(1, 2), true);
11+
assert.equal(isProperFraction(0, 5), true);
12+
assert.equal(isProperFraction(-1, 2), true);
13+
assert.equal(isProperFraction(1, -2), true);
14+
assert.equal(isProperFraction(-1, -2), true);
15+
});
16+
17+
test("Basic improper fraction", () => {
18+
assert.equal(isProperFraction(5, 2), false);
19+
assert.equal(isProperFraction(3, 3), false);
20+
assert.equal(isProperFraction(5, 0), false);
21+
assert.equal(isProperFraction(-5, 2), false);
22+
assert.equal(isProperFraction(5, -2), false);
23+
assert.equal(isProperFraction(-5, -2), false);
1224
});

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,77 @@ test("Valid single-digit card", () => {
99
assert.equal(getCardValue("9♠"), 9);
1010
});
1111

12+
test("Ace returns 11", () => {
13+
assert.equal(getCardValue("A♠"), 11);
14+
});
15+
16+
test("Face cards return 10", () => {
17+
assert.equal(getCardValue("J♣"), 10);
18+
assert.equal(getCardValue("Q♦"), 10);
19+
assert.equal(getCardValue("K♥"), 10);
20+
})
21+
22+
test("Number cards 2 to 10 return their numbers", () => {
23+
for(let n = 2; n <= 10; n++) {
24+
assert.equal(getCardValue(`${n}♦`), n);
25+
}
26+
});
27+
28+
test("Only valid double-digit '10' card", () => {
29+
assert.equal(getCardValue("10♥"), 10);
30+
});
31+
32+
test("Accepts all four suits", () => {
33+
for (const suit of ["♠", "♥", "♦", "♣"]){
34+
assert.equal(getCardValue(`5${suit}`), 5);
35+
}
36+
});
37+
1238
test("Arbitrary non-card string", () => {
1339
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
1440
});
1541

1642
// TODO: What other invalid card cases can you think of?
43+
test("Missing suit", () => {
44+
assert.throws(() => getCardValue("7"), /Expected a number followed by a suit/);
45+
assert.throws(() => getCardValue("A"), /Expected a number followed by a suit/);
46+
});
47+
48+
test("Missing ranks", () => {
49+
assert.throws(() => getCardValue("♥"), /Expected a number followed by a suit/);
50+
});
51+
52+
test("Empty strings", () => {
53+
assert.throws(() => getCardValue(""), /Expected a number followed by a suit/);
54+
});
55+
56+
test("Out-of-range number ranks", () => {
57+
for (const OOR of ["0♥", "1♠", "11♦", "100♣"]) {
58+
assert.throws(() => getCardValue(OOR), /Expected a number followed by a suit/);
59+
}
60+
});
61+
62+
test("Unknown suit", () => {
63+
assert.throws(() => getCardValue("A❤"), /Expected a number followed by a suit/);
64+
assert.throws(() => getCardValue("AV"), /Expected a number followed by a suit/);
65+
});
66+
67+
test("Lowercase ranks", () => {
68+
assert.throws(() => getCardValue("a♦"), /Expected a number followed by a suit/);
69+
assert.throws(() => getCardValue("k♥"), /Expected a number followed by a suit/);
70+
});
71+
72+
test("Suit before rank", () => {
73+
assert.throws(() => getCardValue("♣A"), /Expected a number followed by a suit/);
74+
});
75+
76+
test("Extra characters or spaces", () => {
77+
for (const ExS of [" A♦", "A♦ ", "A♦♦", "A ♦"]){
78+
assert.throws(() => getCardValue(ExS), /Expected a number followed by a suit/);
79+
}
80+
});
81+
82+
test("Non-string input", () => {
83+
assert.throws(() => getCardValue(undefined), /Expected a number followed by a suit/);
84+
assert.throws(() => getCardValue(5), /Expected a number followed by a suit/);
85+
});

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,17 @@
1717
// execute the code to ensure all tests pass.
1818

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

‎implement-and-rewrite-tests/implement/2-is-proper-fraction.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
// execute the code to ensure all tests pass.
1515

1616
export function isProperFraction(numerator, denominator) {
17-
// TODO: Implement this function
17+
if(Math.abs(numerator) < Math.abs(denominator)){
18+
return true;
19+
} else {
20+
return false;
21+
}
1822
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,31 @@
2424
// execute the code to ensure all tests pass.
2525

2626
export function getCardValue(card) {
27-
// TODO: Implement this function
27+
const validSuits = ["♠", "♥", "♦", "♣"];
28+
const numberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
29+
const faceRanks = ["J", "Q", "K"];
30+
31+
const error = new Error(`Expected a number followed by a suit, but got "${card}"`);
32+
33+
if(typeof card !== "string" || card.length < 2){
34+
throw error;
35+
}
36+
37+
const suit = card.slice(-1);
38+
const rank = card.slice(0, -1);
39+
40+
if(!validSuits.includes(suit)){
41+
throw error;
42+
}
43+
if(rank === "A") {
44+
return 11;
45+
}
46+
if(faceRanks.includes(rank)){
47+
return 10;
48+
}
49+
if(numberRanks.includes(rank)){
50+
return Number(rank);
51+
}
52+
throw error;
2853
}
54+

0 commit comments

Comments
 (0)