Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 6 additions & 16 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
function countChar(stringOfCharacters, findCharacter) {
// Ensure valid inputs
if (
typeof stringOfCharacters !== "string" ||
typeof findCharacter !== "string"
) {
return 0;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove the check instead of strengthening it?

// Count occurrences
let count = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
let total = 0;
for (let i=0; i < stringOfCharacters.length; i++){
if (findCharacter == stringOfCharacters[i]){
total++;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Why replace the for-of loop with a for loop? The former is more compact.

Copy link
Author

Choose a reason for hiding this comment

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

@cjyuan I reverted back

Copy link
Contributor

Choose a reason for hiding this comment

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

That does not answer my question.

return count;
}
return total;
}

module.exports = countChar;
40 changes: 18 additions & 22 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
function getOrdinalNumber(num) {
// Ensure the input is a valid number
if (typeof num !== "number" || isNaN(num)) {
return "";
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove the check instead of strengthening it?

Copy link
Author

Choose a reason for hiding this comment

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

@cjyuan I was just trying to simplify the code

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

let result;

// Handle special cases: 11th, 12th, 13th
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return `${num}th`;
if (num % 100 == 11 || num % 100 == 12 || num % 100 == 13){
result = num.toString() + "th";
}

// Handle normal ordinal endings
switch (lastDigit) {
case 1:
return `${num}st`;
case 2:
return `${num}nd`;
case 3:
return `${num}rd`;
default:
return `${num}th`;
else if (num % 10 == 1){
result = num.toString() +"st";
}
else if (num % 10 == 2){
result = num.toString() + "nd";
}
}
else if (num % 10 == 3){
result = num.toString() + "rd";
}
else {
result = num.toString() + "th";
}

return result
}

module.exports = getOrdinalNumber;
19 changes: 10 additions & 9 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
function repeat(str, count) {
// Validate inputs
if (typeof str !== "string") {
throw new Error("First argument must be a string");
}

if (typeof count !== "number" || count < 0) {
throw new Error("Count must be a non-negative number");
}
if (count < 0) {
return null;
} else if (count == 0) {
return "";
}
let result = "";
for (let i = 0; i < count; i++) {
result += str;
}
return result;

// Repeat string count times
return str.repeat(count);
}

module.exports = repeat;
23 changes: 11 additions & 12 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ 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 return the original string when count is 1", () => {
const str = "hi";
test("handle Count of 1", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hi");
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 return an empty string when count is 0", () => {
const str = "test";
test("handle Count of 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
Expand All @@ -42,10 +42,9 @@ test("should return an empty string when count is 0", () => {
// 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 when count is negative", () => {
const str = "error";
const count = -2;
expect(() => repeat(str, count)).toThrow(
"Count must be a non-negative number"
);
});
test("Negative Count", () => {
const str = "hello";
const count = -1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual(null);
});
Loading