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
10 changes: 9 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let variable = 0; // moteghayer baraye shomaresh

for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
variable++;
}
}

return variable;
}

module.exports = countChar;
8 changes: 8 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// implement a function countChar that counts the number of times a character occurs in a string
const countChar = require("./count");

// Given a string str and a single character char to search for,
// When the countChar function is called with these inputs,
// Then it should:
Expand All @@ -22,3 +23,10 @@ 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 no occurrences are found", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
14 changes: 13 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,17 @@
function getOrdinalNumber(num) {
return "1st";
// Handle the 'teen' exceptions (11, 12, 13) that always end in 'th'
if (num % 100 >= 11 && num % 100 <= 13) {
return num + "th";
}

// Identify the last digit to determine the correct suffix for all other numbers
const lastDigit = num % 10;

if (lastDigit === 1) return num + "st";
if (lastDigit === 2) return num + "nd";
if (lastDigit === 3) return num + "rd";

return num + "th";
}
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 works for all numbers up to 20. Even better would be if you can make it work for all numbers. Right now 21 would output 21th instead of 21st and so on

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.

@noahg9 I've refactored the function using the modulo operator to ensure it's scalable for all numbers. It now passes the new test cases. Thanks for the feedback!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perfect!


module.exports = getOrdinalNumber;
37 changes: 31 additions & 6 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
const getOrdinalNumber = require("./get-ordinal-number");
// In this week's prep, we started implementing getOrdinalNumber

// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback
// Given a number,
// When the getOrdinalNumber function is called with this input,
// Then it should return the correct ordinal string.

// 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", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

// Case 2: Identify the ordinal number for 2
test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});

// Case 3: Identify the ordinal number for 3
test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});

// Case 4: Handle other numbers
test("should return 'th' for numbers other than 1, 2, or 3", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add tests for numbers ending in 1, 2 or 3 after 20. They will fail with your current implementation

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.

@noahg9 I've added the test cases for numbers ending in 1, 2, and 3 after 20 as requested. They are now passing with the updated logic in the main file.

// Case 5: Handle numbers ending in 1, 2, or 3 after 20
test("should return '21st' for 21", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
});

test("should return '22nd' for 22", () => {
expect(getOrdinalNumber(22)).toEqual("22nd");
});

test("should return '23rd' for 23", () => {
expect(getOrdinalNumber(23)).toEqual("23rd");
});
14 changes: 12 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (count < 0) {
throw new Error("Count must be a non-negative integer");
}

let variable = ""; // inja natije ro jam mikonim

for (let i = 0; i < count; i++) {
variable += str;
}

return variable;
}

module.exports = repeat;
14 changes: 14 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,26 @@ 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 original string when count is 1", () => {
expect(repeat("hello", 1)).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", () => {
expect(repeat("hello", 0)).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 throw an error when count is negative", () => {
expect(() => {
repeat("hello", -1);
}).toThrow("Count must be a non-negative integer");
});