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 count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;
47 changes: 47 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice job on these tests!

Since you are already thinking about edge cases like that, what are some other tricky inputs we might want to test for? (For example: what happens if someone passes in an empty string, or wants to count empty spaces?

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,54 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character", () => {
const str = "Code your Future";
const char = "u";
const count = countChar(str, char);
expect(count).toEqual(3);
});

test("should count multiple occurrences of a character", () => {
const str = "Introduction to Programming";
const char = "o";
const count = countChar(str, char);
expect(count).toEqual(4);
});

test("should count multiple occurrences of a character", () => {
const str = "Introduction to Javascript";
const char = "i";
const count = countChar(str, char);
expect(count).toEqual(2);
});
// Scenario: No Occurrences

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You have the setup comments for a 'No Occurrences' scenario, but the test block is missing. maybe you've forgotten to write this one?

test("should return 0 when the character does not occur in the string", () => {
const str = "No occurrences here";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
// for empty string scenario.
test("should return 0 when the input string is empty", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
//Scenario: for empty find character.
test("should return 0 when the find character is empty", () => {
const str = "empty find character";
const char = "";
const count = countChar(str, char);
expect(count).toEqual(0);
});
// Scenario: counting empty spaces in a string.
test("should return the count of empty spaces in a string", () => {
const str = "The quick brown fox jumps over the lazy dog";
const char = " ";
const count = countChar(str, char);
expect(count).toEqual(8);
});
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
Expand Down
5 changes: 4 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,8 @@
function getOrdinalNumber(num) {
return "1st";
const s = ["th", "st", "nd", "rd"];
const lastDigits = num % 100;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice job on the logic
Just a quick formatting note: the indentations here in this file and the others can be improved.
Have you considered setting up a formatter like Prettier in your IDE to handle these automatically?

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.

yeah i do install the prettier extension, just failed to make it auto-format on save. thanks i will fix it.

// Uses the index 1, 2, or 3 for st/nd/rd, defaults to 0 (th)
return num + (s[(lastDigits - 20) % 10] || s[lastDigits] || s[0]);
}

module.exports = getOrdinalNumber;
24 changes: 24 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,27 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

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(132)).toEqual("132nd");
});

test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});

test("should append 'th' for numbers ending with 4-9, 0, and teens (11-13)", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(70)).toEqual("70th");
expect(getOrdinalNumber(40)).toEqual("40th");
});

11 changes: 6 additions & 5 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function repeatStr() {
// 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";
function repeatStr(str, count) {
let result = "";
for (let i = 0; i < count; i++) {
result += str;
}
return result;
}

module.exports = repeatStr;
24 changes: 22 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.test.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Imagine one of these tests fails and you're reading that red output in the console. Since all the descriptions are identical right now (both here and in count.test.js), how could we update the test names to be more self-explanatory so you immediately know exactly what broke?

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.

you are right this could be fix, maybe by making clear test description.

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,34 @@ const repeatStr = require("./repeat-str");
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.

test("should repeat the string count times", () => {
test("should repeat the string with no spaces 3 times when count is 3", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

// Case: handle count of 1:
test("should repeat the string with no spaces 3 times when count is 3", () => {
const str = "CYF";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("CYFCYFCYF");
});

test("should display the string with spaces one times where count is 1", () => {
const str = "Code Your Future";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("Code Your Future");
});

test("should return empty string when count is 0 times", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// 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.
Expand Down