Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cff3419
wrote a test to count a character in a string where that character do…
MaharitB Oct 16, 2026
e3a0787
edited the test
MaharitB Oct 16, 2026
6cd3097
made a change to the code, test passed
MaharitB Oct 16, 2026
5418d11
wrote a test , test failed.
MaharitB Oct 16, 2026
86ffa32
added some line in the code to make the test pass, two more test failed
MaharitB Oct 16, 2026
a23a6a5
added some code to make the test pass,test passed
MaharitB Oct 17, 2026
4fffb00
wrote a test and it passed
MaharitB Oct 17, 2026
0b8b953
added a test which includes number in the string,test passed
MaharitB Oct 17, 2026
623a0a6
added a test for a £ character,test passed
MaharitB Oct 17, 2026
cbd3ff3
added a test that would count space between words in a string
MaharitB Oct 17, 2026
e0136e6
added a test to count characters in group of words in a string, test …
MaharitB Oct 17, 2026
4a71d58
test case 1 failed, fixed the code, test passed.
MaharitB Oct 17, 2026
68e09e3
wrote a test for case 2. Test failed
MaharitB Oct 17, 2026
9b5152e
wrote a code to pass the test.test passed
MaharitB Oct 17, 2026
dd1a452
wrote a test for case 3, test failed.
MaharitB Oct 17, 2026
650bd92
two tests failed so made amend on the code and one test passed,one te…
MaharitB Oct 17, 2026
a7c42ab
make changes to the test code and test passed
MaharitB Oct 17, 2026
7f7deb4
add atest to the testing file and test failed
MaharitB Oct 17, 2026
5b59e17
added and amended in the code and the test passed
MaharitB Oct 17, 2026
494887f
added a test for case 5, exception numbers, test failed.
MaharitB Oct 17, 2026
4453a23
made changes to the code and test passed on case 5.
MaharitB Oct 17, 2026
91c2378
added a test to throw an error when the argument is 0. Test failed
MaharitB Oct 17, 2026
87eb71e
added a code that throws an error when num is passed with a value zer…
MaharitB Oct 17, 2026
55e6ea0
wrote a test with count =2, test failed
MaharitB Oct 17, 2026
2a2811a
added a code to make the test failed pass. Success.
MaharitB Oct 17, 2026
119aabb
added a test for the case count=1, test failed
MaharitB Oct 17, 2026
e8ae9bb
added a code to make the test pass four count =1, test passed
MaharitB Oct 17, 2026
c89337c
added a test for case where count=0, test failed
MaharitB Oct 17, 2026
a87428c
made a small change to the test file on the expected string
MaharitB Oct 17, 2026
ad818d2
wrote a code for case count=0, and test passed
MaharitB Oct 17, 2026
0f414c7
added a test for -ve counts and the code will throw an error.test failed
MaharitB Oct 17, 2026
2e069e9
count<0 in code is added and the test passed by throwing error when c…
MaharitB Oct 17, 2026
0cbd674
added a test that would return 4 hello.test failed
MaharitB Oct 17, 2026
b6e57ae
added a code that woud repeat a string count time. test passed
MaharitB Oct 17, 2026
6fd92ce
removed duplicated codes(refractor time)
MaharitB Oct 17, 2026
79f8acd
refractor code
MaharitB Oct 17, 2026
20e8438
cleaning up the code and format using prettier
MaharitB Oct 17, 2026
217ef19
cleaning up the code
MaharitB Oct 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] == findCharacter[0]) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing each character to findCharacter[0] works well when the target is a single character, as the exercise describes. It might be interesting to try passing something like "an" and see what happens. You could then decide whether the function should handle that case or whether it’s outside what the function promises, and add a test that documents your decision. A test for an empty string could also be a helpful way to confirm the behaviour you expect.

count = count + 1;
}
}
return count;
}
55 changes: 44 additions & 11 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
// implement a function countChar that counts the number of times a character occurs in a string
import { countChar } from "./count.js";
// Given a string `str` and a single character `char` to search for,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Given/When/Then comments you removed describe each scenario in plain language, so consider restoring them and writing your own for the new scenarios you’ve added.

// When the countChar function is called with these inputs,
// Then it should:

// Scenario: Multiple Occurrences
// Given the input string `str`,
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
Expand All @@ -18,7 +11,47 @@ test("should count multiple occurrences of a character", () => {
});

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.

test(" Should count multiple occurences of character", () => {
const str = "aaaaa";
const char = "c";
const count = countChar(str, char);
expect(count).toEqual(0);
});
Comment on lines +15 to +20

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several of the test names are identical, and a few don’t quite match what the test checks. For example, line 15 checks for zero occurrences, while lines 22 and 28 check for one. Since the name is the first clue when a test fails, it might help to give each one a name that describes its exact scenario.


test("should count multiple occurences of character", () => {
const str = "acc";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(1);
});
test("should count multiple occurences of character", () => {
const str = "AAAa";
const char = "a";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea to include this test. It shows that only the lowercase “a” is counted and the uppercase “A”s are ignored, so your function is case-sensitive. It might be helpful to rename the test to reflect that, perhaps by mentioning uppercase and lowercase.

const count = countChar(str, char);
expect(count).toEqual(1);
});
test("should count multiple occurences of character", () => {
const str = "11aa";
const char = "1";
const count = countChar(str, char);
expect(count).toEqual(2);
});
test("should count multiple occurences of character", () => {
const str = "£100";
const char = "£";
const count = countChar(str, char);
expect(count).toEqual(1);
});
test("should count multiple occurences of character", () => {
const str = "let's play tag";
const char = " ";
const count = countChar(str, char);
expect(count).toEqual(2);
});
test("should count multiple occurences of character", () => {
const str = "let's play tag";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(2);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can consider adding tests for an empty string and for a target longer than one character (like "an"), then decide whether the function should handle that or if it’s outside what the function promises.

18 changes: 17 additions & 1 deletion practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export function getOrdinalNumber(num) {
return "1st";
let numStr = num.toString();
if (num == 0) {
throw new Error("Invalid Input");
}
if (numStr.slice(-1) == "1" && num != 11) {
return `${num}st`;
}
if (numStr.slice(-1) == "2" && num != 12) {
return `${num}nd`;
}
if (numStr.slice(-1) == "3" && num != 13) {
return `${num}rd`;
}
if (num == 11 || num == 12 || num == 13) {
return `${num}th`;
}
return `${num}th`;
Comment on lines +6 to +18

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good approach using the last digit to choose the suffix. The special-case check compares the whole number (e.g. num != 11), so it might be worth trying 111, 112, and 113 to see what your function returns. Have a think about which part of the number decides whether the “teen” rule applies. It also looks like lines 15–17 may not be needed, since line 18 would return the same result for 11, 12 and 13. Finally, it would be helpful to decide what the function should do with 0, negative numbers, and decimals.

}
43 changes: 31 additions & 12 deletions practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import { getOrdinalNumber } from "./get-ordinal-number.js";
// In this week's prep, we started implementing getOrdinalNumber.

// Continue testing and implementing getOrdinalNumber for additional cases.
// Write your tests using Jest — remember to run your tests often for continual feedback.

// To ensure thorough testing, we need broad scenarios that cover all possible cases.
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
// Instead of writing tests for individual numbers, consider grouping all possible input values
// into meaningful categories. Then, select representative samples from each category to test.
// This approach improves coverage and makes our tests easier to maintain.

// Case 1: Numbers ending with 1 (but not 11)
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.
Comment on lines -2 to -15

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The instructions you removed explain the testing approach (grouping inputs into categories) and the Given/When/Then for Case 1, so consider restoring them as documentation.

.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove

test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});
// Case 2: Numbers ending with 2(but not 12).
test("should append 'nd' for numbers ending with 1, except those ending with 11", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test checks the “nd” suffix, but the name still mentions numbers ending with 1, except those ending with 11. It looks like it may have been copied from Case 1, so it might be worth updating the name to match what this test checks.

expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});
//Case 3: Number ending with 3(but not 13)
test("should append 'rd' for numbers ending with 1, except those ending with 11", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as line 11

expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});
//case 4 : Number ending between 4 and 9 including (0,4,9)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment says “between 4 and 9 including (0,4,9)”, which is a little confusing, so could you reword it to match the test name on line 23?

test("should append 'th' for numbers ending with (4,5,6,7,8,9,0)", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(15)).toEqual("15th");
expect(getOrdinalNumber(58)).toEqual("58th");
expect(getOrdinalNumber(100)).toEqual("100th");
});
//Case 5: the exception numbers left out in all the cases(11,12,13)
test("should append 'th' for numbers 11, 12, 13", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try adding a few to this test and see what your function returns.

expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});
//case 5 now we have come to the last one 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is labelled “case 5” again, so check the numbering.

test(` should throw an error when num is 0`, () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work covering the main cases. One more you might like to explore is what happens when str is an empty string. Adding a test for it could be a helpful addition.

expect(() => getOrdinalNumber(0)).toThrow(`Invalid Input`);
});
16 changes: 12 additions & 4 deletions practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export function repeatStr() {
// 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).
// The goal is to re-implement that function, not to use it.
Comment on lines -1 to -3

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed comment explained that the function must not use String.prototype.repeat, which is a key rule of the exercise, so consider keeping it.

return "hellohellohello";
export function repeatStr(str, count) {
let repeatedStr = str;
if (count < 0) {
throw new Error("Negative counts are not valid");
Comment on lines +3 to +4

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work handling the negative count with a clear error message. What happens if count is a decimal, like 2.5? Decide what the function should do and add a test for it.

}
if (count == 0) {
return " ";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check what this returns?

}
for (let i = 1; i < count; i++) {
repeatedStr += str;
}
return repeatedStr;
}
45 changes: 28 additions & 17 deletions practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
// Implement a function repeatStr
import { repeatStr } from "./repeat-str.js";
// Given a target string `str` and a positive integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should:

// Case: handle multiple repetitions:
// Given a target string `str` and a positive integer `count` greater than 1,
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.
Comment on lines -3 to -10

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Given/When/Then comments you removed are the specifications for each case, so consider restoring them.


test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});
test("should repeat the string count times", () => {
const str = "hello";
const count = 2;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohello");
});
test("should repeat the string count times", () => {
const str = "hello";
const count = 4;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohellohello");
});
Comment on lines +10 to +21

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the tests currently share the name “should repeat the string count times”, which could make it hard to tell which one failed. It might help to name each test after the case it covers, for example, a count of 1, a count of 0 or a negative count.


// Case: handle count of 1:
// Given a target string `str` and a `count` equal to 1,
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.

test("should repeat the string count times", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});
// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.

test("should repeat the string count times", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual(" ");
Comment on lines +35 to +37

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original comment for this case mentioned that the function should return an empty string when count is 0. Could you take another look at what this test expects? It’s worth checking whether it matches the specification or the current behaviour of your code.

});
// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the extra blank line here

test("should repeat the string count times", () => {
expect(() => repeatStr("hello", -1)).toThrow("Negative counts are not valid");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be worth thinking about what should happen when str is an empty string. You might consider adding a test for that case, too.

Loading