Skip to content

Commit 2a4f7bf

Browse files
committed
Filled in 2-practice-tdd
1 parent 3372770 commit 2a4f7bf

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

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

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

510
module.exports = countChar;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
25+
test("should count no occurrences of a character", () => {
26+
const str = "FFFFF"
27+
const char= "z"
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
if (lastDigit === 1 && lastTwoDigits !== 11) {
5+
return num + "st";
6+
}
7+
else if (lastDigit === 2 && lastTwoDigits !== 12) {
8+
return num + "nd";
9+
}
10+
else if (lastDigit === 3 && lastTwoDigits !== 13) {
11+
return num + "rd"
12+
}
13+
return num + "th";
314
}
415

516
module.exports = getOrdinalNumber;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ 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+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(142)).toEqual("142nd");
26+
});
27+
28+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
29+
expect(getOrdinalNumber(3)).toEqual("3rd");
30+
expect(getOrdinalNumber(33)).toEqual("33rd");
31+
expect(getOrdinalNumber(153)).toEqual("153rd");
32+
});
33+
34+
test("should append 'th' for the numbers 11, 12, and 13", () => {
35+
expect(getOrdinalNumber(11)).toEqual("11th");
36+
expect(getOrdinalNumber(12)).toEqual("12th");
37+
expect(getOrdinalNumber(13)).toEqual("13th");
38+
});
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Negative counts are not allowed.")
4+
}
5+
let result = "";
6+
for (let i = 0; i < count; i++) {
7+
result += str;
8+
}
9+
return result;
310
}
411

512
module.exports = repeatStr;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,30 @@ 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 print the string once", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2329

2430
// Case: Handle count of 0:
2531
// Given a target string `str` and a `count` equal to 0,
2632
// When the repeatStr function is called with these inputs,
2733
// Then it should return an empty string.
34+
test("should print nothing", () => {
35+
const str = "";
36+
const count = 0;
37+
const repeatedStr = repeatStr(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// Case: Handle negative count:
3042
// Given a target string `str` and a negative integer `count`,
3143
// When the repeatStr function is called with these inputs,
3244
// Then it should throw an error, as negative counts are not valid.
45+
test("should error if using negatives", () => {
46+
const str = "hello";
47+
const count = -5;
48+
expect(() => repeatStr(str, count)).toThrow();
49+
});

0 commit comments

Comments
 (0)