Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7b93eed
Add test case for character not found in string
alexandru-pocovnicu Oct 16, 2025
86a4f93
Implement character counting logic in countChar function
alexandru-pocovnicu Oct 16, 2025
7429d54
Added tests for repeat function handling various counts
alexandru-pocovnicu Oct 16, 2025
a59de40
Implement repeat function to handle string repetition with error hand…
alexandru-pocovnicu Oct 16, 2025
9af1dec
added tests for additional getordinalnumber function
alexandru-pocovnicu Oct 16, 2025
de7beb8
added code to the getordinalfunction to cover all possibilities
alexandru-pocovnicu Oct 16, 2025
dd1f125
added test to check for negative numbers
alexandru-pocovnicu Oct 16, 2025
a2ae864
refactor countChar function for improved readability
alexandru-pocovnicu Oct 24, 2025
cf3d5cd
fix countChar function to handle empty character cases and improve it…
alexandru-pocovnicu Oct 26, 2025
7f9d151
add additional tests for countChar function to handle empty character…
alexandru-pocovnicu Oct 26, 2025
253d984
fix getOrdinalNumber function to handle invalid inputs and improve lo…
alexandru-pocovnicu Oct 26, 2025
0f7f656
add tests for getOrdinalNumber function to handle NaN cases and addit…
alexandru-pocovnicu Oct 26, 2025
ade9346
refactor repeat function for improved formatting and readability
alexandru-pocovnicu Oct 26, 2025
a1e9f60
add tests for repeat function to handle empty strings, strings with s…
alexandru-pocovnicu Oct 26, 2025
5d5d095
removed scratch work
alexandru-pocovnicu Oct 26, 2025
6c0be54
updated the function to handle empty strings
alexandru-pocovnicu Oct 26, 2025
1a64458
updated test case description
alexandru-pocovnicu Oct 26, 2025
eee22d9
update test descriptions for clarity on invalid inputs
alexandru-pocovnicu Oct 26, 2025
a4b2b74
refactor getOrdinalNumber function to improve input validation structure
alexandru-pocovnicu Oct 26, 2025
90a7852
fix formatting in getOrdinalNumber function for consistency
alexandru-pocovnicu Oct 27, 2025
7198839
update tests for repeat function to handle undefined count and improv…
alexandru-pocovnicu Oct 27, 2025
b91f950
refactor repeat function to improve input validation and error handling
alexandru-pocovnicu Oct 27, 2025
9c86871
refactor repeat function to enhance input validation for string type
alexandru-pocovnicu Oct 27, 2025
e28e199
update tests for repeat function to handle non-string input and array…
alexandru-pocovnicu Oct 27, 2025
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: 11 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let char = 0;
for(let i = 0;i <= stringOfCharacters.length - findCharacter.length;i++){
if ( findCharacter.length === 0 || stringOfCharacters === 0){
return 0;
}
if(stringOfCharacters.substring(i, i + findCharacter.length ) === findCharacter){
char++;
}
}
return char
}

module.exports = countChar;
console.log(countChar("aaaaa","a"));
41 changes: 41 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,44 @@ test("should count multiple occurrences of a character", () => {
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
test("should return 0 if the character doesn't appear in the string", () => {
const str = "aaaaa";
const char = "s";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should return 0 if the character is empty", () => {
const str = "aaaaa";
const char = "";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should return 0 if the character doesn't appear in the string", () => {
const str = "aaca";
const char = "ab";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should return 0 if the character doesn't appear in the string", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should return 0 if the character doesn't appear in the string", () => {
const str = "aaca";
const char = "ac";
const count = countChar(str, char);
expect(count).toEqual(1);
});

test("should return 0 if the character doesn't appear in the string", () => {
const str = "a";
const char = "aa";
const count = countChar(str, char);
expect(count).toEqual(0);
});
26 changes: 24 additions & 2 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
function getOrdinalNumber(num) {
return "1st";
}
if (typeof num !== "number" ||!Number.isInteger(num) || num === 0 ) {
return NaN;
}

const lastDigit=Number(num.toString().slice(-1));
const lastTwoDigits=Number(num.toString().slice(-2));


if(lastTwoDigits>=10 && lastTwoDigits<=13){
return `${num}th`;
}

if (lastDigit === 1) {
return `${num}st`;
} else if (lastDigit === 2) {
return `${num}nd`;
} else if (lastDigit === 3) {
return `${num}rd`;
}

return `${num}th`;

}
console.log(getOrdinalNumber(-1))
module.exports = getOrdinalNumber;

57 changes: 57 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,63 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return NaN for 1.5", () => {
expect(getOrdinalNumber(1.5)).toEqual(NaN);
});

test("should return NaN for 0", () => {
expect(getOrdinalNumber(0)).toEqual(NaN);
});

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

test("should return '121st' for 121", () =>{
Copy link
Contributor

Choose a reason for hiding this comment

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

These test cases are good but I think there are more edge cases that you could cover to make sure your function behaves in a way you designed. For example, what happens when you pass 0 or 1.5?

Also, I think you could have more comprehensive test cases for numbers. With the current bug you have 33 will not become 33rd and you could catch this bug if you included more tests for each type of ordinal number. If I were you, I would be adding test cases for the 1 digit number, 2 digit number and 3 digits number for each ordinal number type.

Choose a reason for hiding this comment

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

not sure about the negative numbers, left the test in there anyway

expect(getOrdinalNumber(121)).toEqual("121st")
})

test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});


test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});

test("should return '33rd' for 33", () => {
expect(getOrdinalNumber(33)).toEqual("33rd");
});

test("should return '5th' for 5", () => {
expect(getOrdinalNumber(5)).toEqual("5th");
});

test("should return '1114th' for 1114", () => {
expect(getOrdinalNumber(1114)).toEqual("1114th");
});

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

test("should return '-11th' for -11", () => {
expect(getOrdinalNumber(-11)).toEqual("-11th");
});

test("should return '112th' for 112", () => {
expect(getOrdinalNumber(112)).toEqual("112th");
});

test("should return '11113th' for 11113", () => {
expect(getOrdinalNumber(11113)).toEqual("11113th");
});

test("should return NaN for s", () => {
expect(getOrdinalNumber("s")).toEqual(NaN);
});

test("should return NaN for '9'", () => {
expect(getOrdinalNumber("9")).toEqual(NaN);
});
10 changes: 8 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function repeat() {
return "hellohellohello";
function repeat(str,count) {
if(count<0){
return "Error: negative numbers are not valid";
}
return (str.repeat(count));
}

module.exports = repeat;



21 changes: 21 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,33 @@ test("should repeat the string count times", () => {
// When the repeat function is called with these inputs,
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.

test("should not repeat the string", ()=>{
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, these test cases are good but they are a bit slim. You could add couple of more tests to test different variations of count and try to break your function by adding some tests for edge cases, like in the case you pass str="".

Choose a reason for hiding this comment

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

I added a few more tests for str as for count the requirement says it's a positive integer , no point to test for NaN etc, I also commented out the test for count = 0 , 0 not being a positive integer.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think 0 is a nice corner case you should have in your test case.

Regarding the requirement, yes, there is a test for positive values, but do you see why we actually need that test case? It's not just because requirements say count must be positive. It's because JavaScript's .repeat() throws a RangeError for negative numbers, and your if(count<0) check is safeguarding against that. That's to say, you can add guardrails beyond what requirements specify based on how your function actually works. For example, .repeat() also throws errors for Infinity and behaves weirdly for undefined. Testing these cases helps you figure out what other safeguards you might want to add to make your function more robust.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can think of it another way: you can't repeat something negative times, and that's why you have that unit test. Following the same logic, you also can't repeat something undefined times, right? So whether you're thinking about the function logically or from a technical perspective, there's always a set of inputs it shouldn't work with. Writing test cases for these different scenarios helps you catch those edge cases.

const str = "hello";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hello")
})

// case: Handle Count of 0:
// Given a target string str and a count equal to 0,
// When the repeat function is called with these inputs,
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.

test("should return an empty string", ()=>{
const str = "hello";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
})

// case: Negative Count:
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.

test("should return error", ()=>{
const str="hello";
const count=-1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("Error: negative numbers are not valid");
})