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
32 changes: 30 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
function countChar(fullString,findCharacters) {
let count = 0;
// both of these if statement is for input validation if the input not a string.
if( typeof findCharacters !== "string" || typeof fullString !== "string"){
throw new Error("Invalid input");
};

if (findCharacters.length !== 1) {
throw new Error("findCharacters must be a single character");
}

for (let i = 0; i < fullString.length; i++){
if(fullString[i] === findCharacters){
count++;
}
}
return count;
}

module.exports = countChar;


function assertTest(testInput,testCheck){
console.assert(
testInput === testCheck,
`Expect ${testInput} equal to ${testCheck}`
);
};

assertTest(countChar("whale fat hat cat","a"),4)
assertTest(countChar("I need to lean more and know more","e"),5)
assertTest(countChar("the city centre currently have a carnival","c"),4)
assertTest(countChar("the cruise ship in in transit to south America","s"),4)
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 @@ -22,3 +22,24 @@ test("should count multiple occurrences of a character", () => {
// 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 count of zero occurrences of a character", () =>{
expect(countChar("go home and study","c")).toEqual(0);
});

// Scenario: Case-sensitive matching
// Given a string containing both uppercase and lowercase versions of a letter,
// When the function is called with a lowercase character,
// Then it should only count the lowercase occurrences.

test("should treat character matching as case-sensitive", () => {
expect(countChar("AaAa","A")).toEqual(2);
});

// Scenario: Non-alphabet characters
// Given a string containing digits and symbols,
// When the function is called with a non-alphabet character,
// Then it should correctly count occurrences of that character.

test("should count occurrences of non-alphabet characters", () => {
expect(countChar("1-2-3-1-1","1")).toEqual(3);
});
48 changes: 47 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,51 @@
function getOrdinalNumber(num) {
return "1st";
// This is for num argument validation that go inside the function.
if ( !Number.isInteger(num)) {
throw new Error("Invalid input: the value must be a number");
}

// This allow float number to be round into it nearest integer.
const mainReminder = num % 100;
const secondReminder = num % 10;

if (mainReminder === 11 || mainReminder === 12 || mainReminder === 13) {
return `${num}th`;
}
switch (true) {
case secondReminder === 1:
return `${num}st`;
case secondReminder === 2:
return `${num}nd`;
case secondReminder === 3:
return `${num}rd`;
default:
return `${num}th`;
}
}

module.exports = getOrdinalNumber;

function testAssert(inputNumber, outputNUmber) {
console.assert(
inputNumber === outputNUmber,
`Test failed: expected ${outputNUmber}, but got ${inputNumber}`
);
}
//Basic test of first digit integer
testAssert(getOrdinalNumber(1), "1st");
testAssert(getOrdinalNumber(2), "2nd");
testAssert(getOrdinalNumber(3), "3rd");
testAssert(getOrdinalNumber(4), "4th");
//Test for 11, 12 and 13
testAssert(getOrdinalNumber(11), "11th");
testAssert(getOrdinalNumber(12), "12th");
testAssert(getOrdinalNumber(13), "13th");
//Test for double digit number
testAssert(getOrdinalNumber(21), "21st");
testAssert(getOrdinalNumber(32), "32nd");
testAssert(getOrdinalNumber(43), "43rd");
//Normal number and float number test.
testAssert(getOrdinalNumber(101), "101st");
testAssert(getOrdinalNumber(202), "202nd");
testAssert(() => getOrdinalNumber(1.2), "Invalid input");
testAssert(() => getOrdinalNumber(10.51), "Invalid input");
61 changes: 61 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 @@ -18,3 +18,64 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending with 2 (but not 12)
// When the number ends with 2, except those ending with 12,
// Then the function should return a string by appending "nd".
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(142)).toEqual("142nd");
});

// Case 3: Numbers ending with 3 (but not 13)
// When the number ends with 3, except those ending with 13,
// Then the function should return a string by appending 'rd'.
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(153)).toEqual("153rd");
});

// Case 4: Special cases 11, 12, 13
// When the number ends with 11, 12, or 13,
// Then the function should always append "th".
test("should append 'th' for special cases 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(212)).toEqual("212th");
});

// Case 5: All other numbers that is not end in 1,2 or 3.
// When the number does not end with 1, 2, or 3,
// Then the function should append "th".
test("should append 'th' for all number that don't end in 1,2 or 3", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(100)).toEqual("100th");
expect(getOrdinalNumber(204)).toEqual("204th");
});

// Case 6: Float numbers should be giving a error since the function do nut accept float number input
// When the input is a float,
// Then the function should round it and return the correct ordinal.
test("should return error for invalid input", () => {
expect(() => getOrdinalNumber(1.2)).toThrow("Invalid input");
expect(() => getOrdinalNumber(1.8)).toThrow("Invalid input");
expect(() => getOrdinalNumber(2.5)).toThrow("Invalid input");
expect(() => getOrdinalNumber(10.51)).toThrow("Invalid input");
expect(() => getOrdinalNumber(12.49)).toThrow("Invalid input");
expect(() => getOrdinalNumber(12.5)).toThrow("Invalid input");
Comment on lines +65 to +70
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why test so many decimal numbers with decimal point? If the objective is to show the function should treat any number with decimal points as invalid input, one or two samples should be enough.

Under the "invalid input" category, it would probably be more useful to show what other kinds of input are considered invalid. For examples,

  • "300" -- a string that resembles an integer
  • Infinity
  • NaN

});

// Case 7: Invalid inputs should throw an error
// When the input is not a number,
// Then the function should throw an error.
test("should throw an error for invalid inputs", () => {
expect(() => getOrdinalNumber("10")).toThrow("Invalid input");
expect(() => getOrdinalNumber(null)).toThrow("Invalid input");
expect(() => getOrdinalNumber(undefined)).toThrow("Invalid input");
expect(() => getOrdinalNumber(NaN)).toThrow("Invalid input");
});
Loading
Loading