Skip to content

Commit 208f354

Browse files
committed
Glasgow | 26-ITP-JAN | Prakash Dcosta | Sprint 3 | Practice TDD
Self checklist - [X] I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title - [X] My changes meet the requirements of the task - [X] I have tested my changes - [X] My changes follow the [style guide](https://curriculum.codeyourfuture.io/guides/reviewing/style-guide/) ## Changelist Briefly explain your PR. Have made changes to the file as per the coursework
1 parent 3372770 commit 208f354

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
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 = count + 1;
6+
}
7+
}
8+
return count;
39
}
410

511
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 return 0 when the character is not found", () => {
26+
const str = "hello";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return num + "th";
7+
}
8+
9+
switch (lastDigit) {
10+
case 1:
11+
return num + "st";
12+
case 2:
13+
return num + "nd";
14+
case 3:
15+
return num + "rd";
16+
default:
17+
return num + "th";
18+
}
319
}
420

521
module.exports = getOrdinalNumber;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,34 @@ 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+
// When the number ends with 2, except for those ending with 12,
24+
// Then the function should return a string by appending "nd" to the number.
25+
test("returns 'nd' for 2, 22, 102", () => {
26+
expect(getOrdinalNumber(2)).toBe("2nd");
27+
expect(getOrdinalNumber(22)).toBe("22nd");
28+
expect(getOrdinalNumber(102)).toBe("102nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
// When the number ends with 3, except for those ending with 13,
33+
// Then the function should return a string by appending "rd" to the number.
34+
test("returns 'rd' for 3, 23, 103", () => {
35+
expect(getOrdinalNumber(3)).toBe("3rd");
36+
expect(getOrdinalNumber(23)).toBe("23rd");
37+
expect(getOrdinalNumber(103)).toBe("103rd");
38+
});
39+
40+
// Case 4: All other numbers including umbers ending with 11, 12 or 13
41+
// These are for all other numbers including special cases and should always end with "th",
42+
// Then the function should return a string by appending "th" to the number.
43+
test("returns 'th' for all other numbers including those ending with 11, 12, 13", () => {
44+
expect(getOrdinalNumber(11)).toBe("11th");
45+
expect(getOrdinalNumber(12)).toBe("12th");
46+
expect(getOrdinalNumber(13)).toBe("13th");
47+
expect(getOrdinalNumber(10)).toBe("10th");
48+
expect(getOrdinalNumber(99)).toBe("99th");
49+
expect(getOrdinalNumber(100)).toBe("100th");
50+
51+
});
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
5+
6+
let result = "";
7+
for (let i = 0; i < count; i++) {
8+
result += str;
9+
}
10+
11+
return result;
312
}
413

514
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
@@ -21,12 +21,29 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should return the original string when count is 1", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
30+
2431
// Case: Handle count of 0:
2532
// Given a target string `str` and a `count` equal to 0,
2633
// When the repeatStr function is called with these inputs,
2734
// Then it should return an empty string.
2835

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+
expect(repeatedStr).toEqual("");
41+
});
2942
// Case: Handle negative count:
3043
// Given a target string `str` and a negative integer `count`,
3144
// When the repeatStr function is called with these inputs,
3245
// Then it should throw an error, as negative counts are not valid.
46+
47+
test("should throw an error when count is negative", () => {
48+
expect(() => repeatStr("hello", -1)).toThrow();
49+
});

0 commit comments

Comments
 (0)