Skip to content
15 changes: 14 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let counter = 0;
let index = stringOfCharacters.indexOf(findCharacter);
while (index !== -1) {
counter++;

index = stringOfCharacters.indexOf(findCharacter, index + 1);
}
return counter;
}

module.exports = countChar;
function assertFunction(currentOutput, targetOutput) {
console.assert(
currentOutput === targetOutput,
`expect ${currentOutput} to equal ${targetOutput}`
);
}
31 changes: 31 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,34 @@ 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 the character does not exist in the string ", () => {
const str = "Ahmad Hmedan";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should count a single occurrence when the character appears once",() => {
const str = "Ahmad Hmedan";
const char = "A";
const count = countChar(str, char);
expect(count).toEqual(1);
});
test("should count multiple occurrences when the character appears more than once", () => {
const str = "Ahmad Hmedan";
const char = "m";
const count = countChar(str, char);
expect(count).toEqual(2);
});
test("should return 0 when the string is empty", () => {
const str = "";
const char = "@";
const count = countChar(str, char);
expect(count).toEqual(0);
});
test("should correctly count special characters", () => {
const str = "Ahmadhm@gamil.com";
const char = "@";
const count = countChar(str, char);
expect(count).toEqual(1);
});
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";
if (num <= 0) return "Invalid Input";
if (num % 100 === 11) return `${num}th`; // check the last two digit if it exception 11, 12 or 13
if (num % 100 === 12) return `${num}th`;
if (num % 100 === 13) return `${num}th`;
const remainder = num % 10;
switch (remainder) {
case 1:
return `${num}st`;

case 2:
return `${num}nd`;
case 3:
return `${num}rd`;

default:
return `${num}th`;
}
}

module.exports = getOrdinalNumber;
34 changes: 30 additions & 4 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,36 @@ const getOrdinalNumber = require("./get-ordinal-number");
// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback

// Case 1: Identify the ordinal number for 1
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {

test("append 'st' to numbers ending in 1, except those ending in 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(221)).toEqual("221st");
expect(getOrdinalNumber(191)).toEqual("191st");
});
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");
});
test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
expect( getOrdinalNumber(3 )).toEqual("3rd");
expect( getOrdinalNumber(33 ) ).toEqual("33rd");
expect( getOrdinalNumber(133) ).toEqual("133rd");
});


test("append 'th'to numbers ending in 4-9 or ending with 11,12,13", () => {
expect(getOrdinalNumber(99)).toEqual("99th");
expect(getOrdinalNumber(234)).toEqual("234th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(313)).toEqual("313th");
});


test("should throw Invalid Input for negative numbers or 0", () => {
expect(getOrdinalNumber(-5)).toEqual("Invalid Input");
expect(getOrdinalNumber(0)).toEqual("Invalid Input");
});
22 changes: 20 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
function repeat() {
return "hellohellohello";
function repeat(myString, repeatNumber) {
if (repeatNumber < 0) throw new Error("Repeat count must be a positive number");
// if I use this instead return how I can test it with jest??
return myString.repeat(repeatNumber);
}

module.exports = repeat;

// Note:
// When I wrote this code, I didn’t know there was already a built-in method for this.
// Please review this implementation as well.
//if (repeatNumber < 0) throw new Error("Repeat count must be a positive number"); if I use this instead return how I can test it with jest??

// function repeat(myString, repeatNumber) {
// if (repeatNumber < 0) return "Invalid Input must be a positive number";
// let str = "";
// for (let i = 0; i < repeatNumber; i++) {
// str += myString;
// }
// return str;
// }
Comment on lines +14 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

This works too.

While knowing how to use built-in functions can improve productivity and performance, it is equally important to know how to implement such a function without relying on any built-in function.


//if (repeatNumber < 0) throw new Error("Repeat count must be a positive number");
31 changes: 31 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,43 @@ 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 return the string", () => {
const str = "CYF";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("CYF");
});

// 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 empty when the count is 0", () => {
const str = "CYF";
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 empty when the count is negative", () => {
const str = "CYF";
const count = -2;

expect(() => repeat(str,count)).toThrow("Repeat count must be a positive number");
});
Comment on lines 46 to 51
Copy link
Contributor

Choose a reason for hiding this comment

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

If you modified repeat() to throw an error when count is negative, and you wanted to test if the function can throw an error as expected, you can use .toThrow(). You can find out more about how to use .toThrow() here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)

test("should repeat the string five times when the count is 5", () => {
const str = "CYF";
const count = 5;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("CYFCYFCYFCYFCYF");
});
test("should return an empty string when the str is empty whatever was count equal", () => {
const str = "";
const count = 10;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
});