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
7 changes: 6 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let output = 0;
for( let i=0; i <= stringOfCharacters.length; i++ ) {
if(stringOfCharacters[i] === findCharacter)
output++;
}
return output;
}

module.exports = countChar;
6 changes: 6 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,9 @@ 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 there are no occurrences at all", () => {
const str = "Hello World!";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
24 changes: 23 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Copy link
Contributor

Choose a reason for hiding this comment

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

The function should work for any positive integers.

Can you lookup the rules for representing ordinal numbers?

You should prepare tests in get-ordinal-number.test.js file first before implementing getOrdinalNumber().

For example, we can prepare a test for numbers 2, 22, 132, etc. as

test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
    expect( getOrdinalNumber(2) ).toEqual("2nd");
    expect( getOrdinalNumber(22) ).toEqual("22nd");
    expect( getOrdinalNumber(132) ).toEqual("132nd");
});

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the feedback. I set the tests for the function and modified the function getOrdinalNumber() to meet the requirements and to handle all possible cases.

Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
function getOrdinalNumber(num) {
return "1st";
if (!Number.isInteger(num) || num <= 0) {
throw new Error("Input must be a positive integer.");
}

const lastTwoDigits = num % 100;
const lastDigit = num % 10;

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

switch (lastDigit) {
case 1:
return `${num}st`;
case 2:
return `${num}nd`;
case 3:
return `${num}rd`;
default:
return `${num}th`;
}
return num;
}

//console.log(getOrdinalNumber(21)); // "1st"
module.exports = getOrdinalNumber;
48 changes: 47 additions & 1 deletion 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,52 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
test("Numbers that end with `st`", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(101)).toEqual("101st");
expect(getOrdinalNumber(28491)).toEqual("28491st");
});

// Case 2: "Numbers that end with `nd`
test("Numbers that end with `nd`", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(32)).toEqual("32nd");
expect(getOrdinalNumber(202)).toEqual("202nd");
expect(getOrdinalNumber(4502)).toEqual("4502nd");
});

// Case 3:: "Numbers that end with `rd`
test("Numbers that end with `rd`", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(43)).toEqual("43rd");
expect(getOrdinalNumber(123)).toEqual("123rd");
expect(getOrdinalNumber(6703)).toEqual("6703rd");
});

// Case 4: "Numbers that end with `th`
test("Numbers that end with `th`", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(25)).toEqual("25th");
expect(getOrdinalNumber(1124)).toEqual("1124th");
expect(getOrdinalNumber(9999)).toEqual("9999th");
});

// Case 5: "Numbers that are exceptions (11, 12, 13)
test("Numbers that are exceptions (11, 12, 13)", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

// Case 6: "Non-positive integers (NaN, Infinity, non-integers)
test("should throw an error stating that input must be a positive integer", () => {
expect(() => getOrdinalNumber(NaN)).toThrow("Input must be a positive integer.");
expect(() => getOrdinalNumber(Infinity)).toThrow("Input must be a positive integer.");
expect(() => getOrdinalNumber("hello")).toThrow("Input must be a positive integer.");
expect(() => getOrdinalNumber([0, 1, 2])).toThrow("Input must be a positive integer.");
});
7 changes: 5 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (!Number.isInteger(count) || count < 0) {
throw new Error("Count must be a valid number");
}
return str.repeat(count);
}

module.exports = repeat;
26 changes: 26 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,39 @@ 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 repeat the string only 1 time (no repetition)", () => {
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 repeat the string 0 times ( means the output will be 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 throw an error if count is not a valid number", () => {
const str = "hello";
const count = -8;
expect(() => repeat(str, count)).toThrow("Count must be a valid number");
});

test("should throw an error if count is not a valid number", () => {
const str = "hello";
const count = "abc";
expect(() => repeat(str, count)).toThrow("Count must be a valid number");
});