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
8 changes: 7 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let countTime = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] == findCharacter) {//take each character out from string to compare with the given character. If true, then add one to countTime.
countTime = countTime + 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This definitely works! But

stringOfCharacters.slice(i, i + 1)

is quite a lengthy way to access a single character in a string. Can you think of a simpler way?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Understood. I think I make things complicated again. Thanks!

}
}
return countTime;
}

module.exports = countChar;
22 changes: 22 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,32 @@ test("should count multiple occurrences of a character", () => {
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);

const str2 = "4444444444444444444";
const char2 = "4";
const count2 = countChar(str2, char2);
expect(count2).toEqual(19);

const str3 = "aaabcde";
const char3 = "c";
const count3 = countChar(str3, char3);
expect(count3).toEqual(1);

const str4 = "a2b3c43";
const char4 = "3";
const count4 = countChar(str4, char4);
expect(count4).toEqual(2);
});

// Scenario: No Occurrences
// Given the input string `str`,
// 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 count no occurrence of a character", () => {
const str = "fghij";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});
19 changes: 18 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,22 @@
function getOrdinalNumber(num) {
return "1st";
let onesDigit = Number(String(num).slice(-1)); //extract the figure at the tens digit, and convert it into a number.
let tensDigit;
if (String(num).length > 1) {
tensDigit = Number(String(num).slice(-2, -1)); //extract the figure at the tens digit, and convert it into a number.
}
if (tensDigit === 1) {
return num + "th";
} else {
if (onesDigit === 1) {
return num + "st";
} else if (onesDigit === 2) {
return num + "nd";
} else if (onesDigit === 3) {
return num + "rd";
} else {
return num + "th";
}
}
Comment on lines +4 to +19

@Liam310 Liam310 Jul 14, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a mostly working solution, but with a lot of nested if statements it can be quite hard to follow the logic of it.

A few things to consider:

  • You repeat the same if (tensDigit == 1) { return num + "th" } line, which suggests if the tens digit of the number is 1, you're always doing the same thing. How you might you simplify this so you only need to perform that check once?
  • On line 25 you have an else statement that doesn't contain any logic. What should that statement be doing? Why wasn't it caught by the tests?
  • The logic you have for numbers whose "length" is 1 is very similar to the logic you have for numbers whose "length" is more than 1. You could definitely make this less repetitive!
  • In a lot of places you have used == instead of === - I asked you in a previous submission what the difference was and what would be better - same question applies here!
  • You've got a leftover console.log on line 32 - get rid!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for guiding me on this problem. I am having some insights for how to make this kind of statement in the future.

}

module.exports = getOrdinalNumber;
37 changes: 36 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 @@ -13,8 +13,43 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Numbers ending with 1 (but not 11)
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
test("should append 'st' for numbers ending with 1, except those having figure 1 in tens digit", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

test("should append 'nd' for numbers ending with 2, except those having figure 1 in tens digit", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

test("should append 'rd' for numbers ending with 3, except those having figure 1 in tens digit", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});

test("should append 'th' for other numbers ending with 1,2 or 3, except those having figure 1 in tens digit", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(113)).toEqual("113th");
expect(getOrdinalNumber(213)).toEqual("213th");
expect(getOrdinalNumber(313)).toEqual("313th");
expect(getOrdinalNumber(1113)).toEqual("1113th");
});
Comment on lines +21 to +42

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These tests are pretty good! Though every assertion here ends in 1, 2, or 3. If you added an assertion with a number ending in a 4, what would happen? Would it pass?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I missed the regular case and now added back this test.


test("should append 'th' for other numbers ending other than 1,2 or 3", () => {
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(8)).toEqual("8th");
expect(getOrdinalNumber(9)).toEqual("9th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(57)).toEqual("57th");
expect(getOrdinalNumber(79)).toEqual("79th");
expect(getOrdinalNumber(94)).toEqual("94th");
expect(getOrdinalNumber(266)).toEqual("266th");
expect(getOrdinalNumber(465)).toEqual("465th");
expect(getOrdinalNumber(1070)).toEqual("1070th");
});
12 changes: 10 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
function repeatStr() {
function repeatStr(str, count) {
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
// The goal is to re-implement that function, not to use it.
return "hellohellohello";
let strOutput = "";
if (count >= 0){
for (let i=0 ; i<count ; i++){
strOutput = strOutput + str;
}
return strOutput;
}else{
throw new Error("Negative count is not valid");
}
}

module.exports = repeatStr;
17 changes: 17 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
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 repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.
test("should return the string with no reptition", () => {
const str = "morning";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("morning");
});

// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.
test("should return empty string as the count is zero", () => {
const str = "afternoon";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.
test("should throw an error as a negative count is not valid", () => {
expect(() => {
repeatStr("evening", -1);
}).toThrow();
});
Loading