Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 10 additions & 2 deletions Sprint-3/2-practice-tdd/count.js

@hackertainment hackertainment Aug 7, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

semicolon missing in line 8 and line 11.

fun fact: In the original design of JavaScript, there was no semicolon, but it has been added later on - so that nowadays JS programmers generally follow the practice of adding semicolon.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, done that fix

Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// function countChar(stringOfCharacters, findCharacter) {
// return 5
// }
function countChar(stringOfCharacters, findCharacter) {
return 5
let countChar = 0;
for (let stringNum = 0; stringNum < stringOfCharacters.length; stringNum++) {
if (findCharacter === stringOfCharacters[stringNum]){
countChar++;
}
}
return countChar;
}

module.exports = countChar;
21 changes: 21 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,29 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character with spaces", () => {
const str = "eee ee e e";
const char = "e";
const count = countChar(str, char);
expect(count).toEqual7;
});

test("should count multiple occurrences of a character with random characters", () => {
const str = "ajhyabhaa kaak";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(6);
})

// 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 return 0 with no occurrence of the character", () => {
const str ="abcd";
const char = "e";
const count = countChar(str, char);
expect(count).toEqual(0);
});
Comment on lines 34 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There should be one more boundary case making char has no occurrence in str. Can you think of that special case?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

wracked my brain for this one and hit a blank, and advice would be great


11 changes: 10 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think the question requires to also handle 2nd and 3rd etc. Since the question itself may not be quite clear about that, you may clarify in slack channel. Thanks.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will ask on there and if needed update the code with it. ^_^

Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// function getOrdinalNumber(num) {
// return "1st";
// }

function getOrdinalNumber(num) {
return "1st";
let text = num.toString();
if (text === "11") {
return (text + "th");
}
return (text + "st");
}

// getOrdinalNumber(1)).toEqual("1st")
module.exports = getOrdinalNumber;
5 changes: 5 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if the question requires to also handle 2nd and 3rd etc, then there will be more test cases.

Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

test("should append 'th' for number 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");

});
16 changes: 13 additions & 3 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
function repeatStr() {
function repeatStr(str, count) {
let result = "";
if (count < 0) {
throw new Error("cannot be negative number")
}

for (let i=0; i < count; i++) {
result = result + str
}
// 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.
return "hellohellohello";
}
return result;

};

module.exports = repeatStr;

20 changes: 20 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,33 @@ test("should repeat the string count times", () => {
// 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 return the string as the str for counting 1 time", () => {
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 return empty string when count is 0", ()=> {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");

});

// 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.
test("should throw error when count is negative", ()=> {
const str="hello";
const count =-6;
expect(() => repeatStr(str, count)).toThrow("cannot be negative number");

});