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");
}
Comment on lines +8 to +10
Copy link
Contributor

Choose a reason for hiding this comment

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

Is findCharacters allowed to be more than one characters?


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)
3 changes: 3 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Copy link
Contributor

Choose a reason for hiding this comment

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

Could consider testing a few more samples in this script - higher chance to detect bugs in code.

The original specification did not clearly state whether the character match should be case-sensitive.
Most people would probably assume that it is, but to demonstrate our understanding or clarify the assumption we made,
we could add test cases to convey this. For examples,

  • A case to show that the match is case sensitive
  • A case to show that the function is expected to work also for non-alphabets

Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ 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)
});
47 changes: 46 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,50 @@
function getOrdinalNumber(num) {
return "1st";
// This is for num argument validation that go inside the function.
if(typeof num !== "number" || Number.isNaN(num)){
throw new Error ("Invalid input: the value must be a number");
}

// This allow float number to be round into it nearest integer.
num = Math.round(num);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you decide to round the numbers with decimal places? Why not reject them?


if(num % 100 === 11 ||num % 100 === 12 || num % 100 === 13 ){
return `${num}th`;
}
switch( true ){
case num % 10 === 1 : return `${num}st`;
case num % 10 === 2 : return `${num}nd`;
case num % 10 === 3 : return `${num}rd`;
default : return `${num}th`;
}
Comment on lines +10 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you look up the pros and cons between these two styles of expressing the code?

  if(num % 100 === 11 ||num % 100 === 12 || num % 100 === 13 ){
  const lastTwoDigits = num % 100;
  if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13 ) {

?

}

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), "1st");
testAssert(getOrdinalNumber(10.51), "11th");



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
// When the number does not end with 1, 2, or 3,
// Then the function should append "th".
test("should append 'th' for all other numbers", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

When a test fails with the message "... all other numbers", it may be unclear what "other numbers" actually refers to.
Can you revise the test description to make it more informative?

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 rounded to nearest integer
// When the input is a float,
// Then the function should round it and return the correct ordinal.
test("should round float numbers and return correct ordinal", () => {
expect(getOrdinalNumber(1.2)).toEqual("1st"); // rounds to 1
expect(getOrdinalNumber(1.8)).toEqual("2nd"); // rounds to 2
expect(getOrdinalNumber(2.5)).toEqual("3rd"); // rounds to 3
expect(getOrdinalNumber(10.51)).toEqual("11th"); // rounds to 11
expect(getOrdinalNumber(12.49)).toEqual("12th"); // rounds to 12
expect(getOrdinalNumber(12.5)).toEqual("13th"); // rounds to 13
});

// 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