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

Choose a reason for hiding this comment

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

It might have been a leftover from debugging, but console logs should be removed before submitting your PR.

Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
const arrayOfCharacters = stringOfCharacters.split("");
let count = 0;
for (let index = 0; index < arrayOfCharacters.length; index++) {
if (arrayOfCharacters[index] === findCharacter) {
count += 1;
}
}
//console.log(arrayOfCharacters);
//console.log(count);
return count;
}

module.exports = countChar;
7 changes: 7 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js

Choose a reason for hiding this comment

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

Your test case covers the most basic cases - but can you think of some edge cases you should write tests for?

  • what is count is 2.5?
  • what if the count is null or undefined
  • what if the string is an array or an object instead of a string?

Writing tests to cover edge cases will protect your implementation from unexpected situations and make your code more robust.

Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ 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 when character does not exist in the string", () => {
const str = "abcdefg";
const char = "h";
const count = countChar(str, char);
expect(count).toEqual(0);
});
18 changes: 17 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
function getOrdinalNumber(num) {
return "1st";
const stringNum = String(num);
const paddedLast2Digit = stringNum.padStart(2, "0").slice(-2);
const numberValue = Number(paddedLast2Digit);

if (numberValue >= 11 && numberValue <= 13) {
return `${stringNum}th`;
}
if (stringNum.endsWith("1")) {
return `${stringNum}st`;
}
if (stringNum.endsWith("2")) {
return `${stringNum}nd`;
}
if (stringNum.endsWith("3")) {
return `${stringNum}rd`;
}
return `${stringNum}th`;
}

module.exports = getOrdinalNumber;
60 changes: 60 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 @@ -11,3 +11,63 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});
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 '4th' for 4", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
});
test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
});
test("should return '12th' for 12", () => {
expect(getOrdinalNumber(12)).toEqual("12th");
});
test("should return '13th' for 13", () => {
expect(getOrdinalNumber(13)).toEqual("13th");
});
test("should return '21st' for 21", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
});
test("should return '22nd' for 22", () => {
expect(getOrdinalNumber(22)).toEqual("22nd");
});
test("should return '23rd' for 23", () => {
expect(getOrdinalNumber(23)).toEqual("23rd");
});
test("should return '24th' for 24", () => {
expect(getOrdinalNumber(24)).toEqual("24th");
});
test("should return '111th' for 111", () => {
expect(getOrdinalNumber(111)).toEqual("111th");
});
test("should return '112th' for 112", () => {
expect(getOrdinalNumber(112)).toEqual("112th");
});
test("should return '113th' for 113", () => {
expect(getOrdinalNumber(113)).toEqual("113th");
});
test("should return '121st' for 121", () => {
expect(getOrdinalNumber(121)).toEqual("121st");
});
test("should return '1000011th' for 1000011", () => {
expect(getOrdinalNumber(1000011)).toEqual("1000011th");
});
test("should return '1000002nd' for 1000002", () => {
expect(getOrdinalNumber(1000002)).toEqual("1000002nd");
});
test("should return '0th' for 0", () => {
expect(getOrdinalNumber(0)).toEqual("0th");
});
test("should return '-1st' for -1", () => {
expect(getOrdinalNumber(-1)).toEqual("-1st");
});
test("should return '-2nd' for -2", () => {
expect(getOrdinalNumber(-2)).toEqual("-2nd");
});
test("should return '-3rd' for -3", () => {
expect(getOrdinalNumber(-3)).toEqual("-3rd");
});
4 changes: 2 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
return str.repeat(count);
}

module.exports = repeat;
17 changes: 17 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.test.js

Choose a reason for hiding this comment

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

You have written some good tests cases that cover the main cases. However, can you think of some edge cases that should be tested?

  • what if str is not a string (i.e. a number)
  • what if count is not a number (i.e. "2")
  • what if string is empty or null, what should it return?

Choose a reason for hiding this comment

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

@TzeMingHo For this exercise, it says:
"Given a target string str and a positive integer count,"

This means that a number, boolean, null (basically anything not a string), should be considered invalid input.
Converting a non-string to a string would be considered out of scope for this exercise :)
The count should be a positive integer (1,2,3,4,5, etc) and can't have a decimal.

Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,30 @@ test("should repeat the string count times", () => {
// Given a target string str and a count equal to 1,
// 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 return the string 1 time when count is 1", () => {
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 when count is 0", () => {
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 throw an error when count is negative", () => {
const str = "hello";
const count = -2;
expect(() => repeat(str, count)).toThrow("Invalid count value");
});
Loading