Skip to content

Commit 160e0eb

Browse files
committed
Complete TDD practice exercises
1 parent 06651b9 commit 160e0eb

9 files changed

Lines changed: 153 additions & 125 deletions

File tree

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
// execute the code to ensure all tests pass.
1616

1717
function getAngleType(angle) {
18-
1918
// TODO: Implement this function
2019
}
2120

2221
// The line below allows us to load the getAngleType function into tests in other files.
2322
// This will be useful in the "rewrite tests with jest" step.
2423
module.exports = getAngleType;
2524

26-
2725
// This helper function is written to make our assertions easier to read.
2826
// If the actual output matches the target output, the test will pass
2927
function assertEquals(actualOutput, targetOutput) {
@@ -35,23 +33,5 @@ function assertEquals(actualOutput, targetOutput) {
3533

3634
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3735
// Example: Identify Right Angles
38-
39-
// Acute angles
40-
assertEquals(getAngleType(45), "Acute angle");
41-
42-
// Right angle
43-
assertEquals(getAngleType(90), "Right angle");
44-
45-
// Obtuse angle
46-
assertEquals(getAngleType(120), "Obtuse angle");
47-
48-
// Straight angle
49-
assertEquals(getAngleType(180), "Straight angle");
50-
51-
// Reflex angle
52-
assertEquals(getAngleType(270), "Reflex angle");
53-
54-
// Invalid angles
55-
assertEquals(getAngleType(0), "Invalid angle");
56-
assertEquals(getAngleType(360), "Invalid angle");
57-
assertEquals(getAngleType(-45), "Invalid angle");
36+
const right = getAngleType(90);
37+
assertEquals(right, "Right angle");

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

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

1313
function isProperFraction(numerator, denominator) {
14-
if (denominator === 0) {
15-
return false;
16-
}
17-
return numerator < denominator;
14+
// TODO: Implement this function
1815
}
16+
1917
// The line below allows us to load the isProperFraction function into tests in other files.
2018
// This will be useful in the "rewrite tests with jest" step.
2119
module.exports = isProperFraction;
@@ -33,22 +31,3 @@ function assertEquals(actualOutput, targetOutput) {
3331

3432
// Example: 1/2 is a proper fraction
3533
assertEquals(isProperFraction(1, 2), true);
36-
37-
38-
// Numerator equals denominator
39-
assertEquals(isProperFraction(2, 2), false);
40-
41-
// Improper fraction
42-
assertEquals(isProperFraction(5, 3), false);
43-
44-
// Zero numerator
45-
assertEquals(isProperFraction(0, 5), true);
46-
47-
// Denominator is zero
48-
assertEquals(isProperFraction(1, 0), false);
49-
50-
// Another proper fraction
51-
assertEquals(isProperFraction(3, 4), true);
52-
53-
// Another improper fraction
54-
assertEquals(isProperFraction(10, 4), false);

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

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,7 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
const rank = card.slice(0, -1);
26-
const suit = card.slice(-1);
27-
28-
const validSuits = ["♠", "♥", "♦", "♣"];
29-
30-
if (!validSuits.includes(suit)) {
31-
throw new Error("Invalid card");
32-
}
33-
34-
if (rank === "A") {
35-
return 11;
36-
}
37-
38-
if (rank === "J" || rank === "Q" || rank === "K") {
39-
return 10;
40-
}
41-
42-
const value = Number(rank);
43-
44-
if (value >= 2 && value <= 10) {
45-
return value;
46-
}
47-
48-
throw new Error("Invalid card");
25+
// TODO: Implement this function
4926
}
5027

5128
// The line below allows us to load the getCardValue function into tests in other files.
@@ -63,21 +40,10 @@ function assertEquals(actualOutput, targetOutput) {
6340
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
6441
// Examples:
6542
assertEquals(getCardValue("9♠"), 9);
66-
assertEquals(getCardValue("2♠"), 2);
67-
assertEquals(getCardValue("10♥"), 10);
68-
69-
// Face cards
70-
assertEquals(getCardValue("J♣"), 10);
71-
assertEquals(getCardValue("Q♦"), 10);
72-
assertEquals(getCardValue("K♥"), 10);
73-
74-
// Ace
75-
assertEquals(getCardValue("A♠"), 11);
7643

7744
// Handling invalid cards
7845
try {
7946
getCardValue("invalid");
80-
8147

8248
// This line will not be reached if an error is thrown as expected
8349
console.error("Error was not thrown for invalid card 😢");
@@ -86,30 +52,3 @@ try {
8652
}
8753

8854
// What other invalid card cases can you think of?
89-
try {
90-
getCardValue("1♠");
91-
console.error("Error was not thrown 😢");
92-
} catch (e) {
93-
console.log("Error thrown 🎉");
94-
}
95-
96-
try {
97-
getCardValue("11♠");
98-
console.error("Error was not thrown 😢");
99-
} catch (e) {
100-
console.log("Error thrown 🎉");
101-
}
102-
103-
try {
104-
getCardValue("A");
105-
console.error("Error was not thrown 😢");
106-
} catch (e) {
107-
console.log("Error thrown 🎉");
108-
}
109-
110-
try {
111-
getCardValue("5X");
112-
console.error("Error was not thrown 😢");
113-
} catch (e) {
114-
console.log("Error thrown 🎉");
115-
}

Sprint-3/2-practice-tdd/count.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
4+
5+
for (let i = 0; i < stringOfCharacters.length; i++) {
6+
if (stringOfCharacters[i] === findCharacter) {
7+
count++;
8+
}
9+
}
10+
return count;
311
}
412

13+
514
module.exports = countChar;

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ const countChar = require("./count");
55
// Then it should:
66

77
// Scenario: Multiple Occurrences
8-
// Given the input string `str`,
9-
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
10-
// When the function is called with these inputs,
11-
// Then it should correctly count occurrences of `char`.
12-
138
test("should count multiple occurrences of a character", () => {
14-
const str = "aaaaa";
15-
const char = "a";
16-
const count = countChar(str, char);
17-
expect(count).toEqual(5);
9+
const str = "aaaaa";
10+
const char = "a";
11+
const count = countChar(str, char);
12+
expect(count).toEqual(5);
1813
});
1914

15+
2016
// Scenario: No Occurrences
21-
// Given the input string `str`,
22-
// And a character `char` that does not exist within `str`.
23-
// When the function is called with these inputs,
24-
// Then it should return 0, indicating that no occurrences of `char` were found.
17+
test("should return 0 when character does not exist in string", () => {
18+
const str = "hello";
19+
const char = "z";
20+
const count = countChar(str, char);
21+
22+
23+
expect(count).toEqual(0);
24+
});
25+
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if (num % 100 >= 11 && num % 100 <= 13) {
3+
return `${num}th`;
4+
}
5+
if (num % 10 === 1) {
6+
return `${num}st`;
7+
}
8+
9+
10+
if (num % 10 === 2) {
11+
return `${num}nd`;
12+
}
13+
14+
15+
if (num % 10 === 3) {
16+
return `${num}rd`;
17+
}
18+
19+
20+
return `${num}th`;
321
}
422

523
module.exports = getOrdinalNumber;

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,67 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
// Case 2: Numbers ending with 2 (but not 12)
23+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
24+
expect(getOrdinalNumber(112)).toEqual("112th");
25+
expect(getOrdinalNumber(32)).toEqual("32nd");
26+
expect(getOrdinalNumber(332)).toEqual("332nd");
27+
});
28+
29+
30+
// Case 3: Numbers ending with 3 (but not 13)
31+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
32+
expect(getOrdinalNumber(113)).toEqual("113th");
33+
expect(getOrdinalNumber(23)).toEqual("23rd");
34+
expect(getOrdinalNumber(453)).toEqual("453rd");
35+
});
36+
37+
38+
// Case 4: Numbers ending with 4 -9
39+
test("should append 'th' for numbers ending with 4", () => {
40+
expect(getOrdinalNumber(124)).toEqual("124th");
41+
expect(getOrdinalNumber(235)).toEqual("235th");
42+
expect(getOrdinalNumber(16)).toEqual("16th");
43+
expect(getOrdinalNumber(17)).toEqual("17th");
44+
expect(getOrdinalNumber(18)).toEqual("18th");
45+
expect(getOrdinalNumber(19)).toEqual("19th");
46+
});
47+
48+
49+
50+
51+
// Case 5: Numbers ending with 11, 12, 13
52+
test("should append 'th' for numbers ending with 11, 12, 13", () => {
53+
expect(getOrdinalNumber(11)).toEqual("11th");
54+
expect(getOrdinalNumber(12)).toEqual("12th");
55+
expect(getOrdinalNumber(13)).toEqual("13th");
56+
expect(getOrdinalNumber(111)).toEqual("111th");
57+
expect(getOrdinalNumber(112)).toEqual("112th");
58+
expect(getOrdinalNumber(113)).toEqual("113th");
59+
});
60+
61+
62+
// Case 6: Numbers ending with 0
63+
test("should append 'th' for numbers ending with 0", () => {
64+
expect(getOrdinalNumber(10)).toEqual("10th");
65+
expect(getOrdinalNumber(20)).toEqual("20th");
66+
expect(getOrdinalNumber(30)).toEqual("30th");
67+
expect(getOrdinalNumber(140)).toEqual("140th");
68+
});
69+
function repeatStr() {
70+
let result = "";
71+
72+
73+
for (let i = 0; i < num; i++) {
74+
result += str;
75+
}
76+
77+
78+
return result;
79+
}
80+
81+
82+
module.exports = repeatStr;
83+
84+
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
function repeatStr() {
2-
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
3-
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
2+
let result = "";
3+
4+
for (let i = 0; i < n; i++) {
5+
result += str;
6+
}
7+
8+
return result;
59
}
610

711
module.exports = repeatStr;

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,47 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23+
test("should return the original string when count is 1", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
28+
29+
expect(repeatedStr).toEqual("hello");
30+
});
2331

2432
// Case: Handle count of 0:
2533
// Given a target string `str` and a `count` equal to 0,
2634
// When the repeatStr function is called with these inputs,
2735
// Then it should return an empty string.
36+
test("should return an empty string when count is 0", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
41+
42+
expect(repeatedStr).toEqual("");
43+
});
2844

2945
// Case: Handle negative count:
3046
// Given a target string `str` and a negative integer `count`,
3147
// When the repeatStr function is called with these inputs,
3248
// Then it should throw an error, as negative counts are not valid.
49+
50+
51+
function repeatStr() {
52+
let result = "";
53+
54+
55+
for (let i = 0; i < num; i++) {
56+
result += str;
57+
}
58+
59+
60+
return result;
61+
}
62+
63+
64+
module.exports = repeatStr;
65+
66+

0 commit comments

Comments
 (0)