Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions Project-CLI-Treasure-Hunt
Submodule Project-CLI-Treasure-Hunt added at 6668c4
2 changes: 1 addition & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ function capitalise(str) {
}

// =============> write your explanation here
// =============> write your new code here
// =============> write your new code here
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// write one test at a time, and make it pass, build your solution up methodically
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
function getCardValue(card) {
if (rank === "A") {
if (card === "A") {
return 11;
}
}
Expand Down
11 changes: 9 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}
return count;
}
console.log(countChar("hello world!", "l"));

module.exports = countChar;
7 changes: 7 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,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 character does not exist in the string", () => {
const str = "hello world";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
9 changes: 8 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,12 @@
function getOrdinalNumber(num) {
return "1st";
const suffixes = ["th", "st", "nd", "rd"];
const date = num % 100;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why named the variable date? The last two digits do not represent a date.

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 pointing that out. yes you are absolutely right! I was in class yesterday and couldn't focus enough. I changed it to a proper Variable name.


if (date >= 11 && date <= 13) {
return num + "th";
}
const suffix = suffixes[(date % 10)] || "th";
return num + suffix;
}

module.exports = getOrdinalNumber;
23 changes: 22 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 @@ -8,6 +8,27 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
test("append 'nd' to numbers ending in 1, except those ending in 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect( getOrdinalNumber(21) ).toEqual("21st");
expect( getOrdinalNumber(131) ).toEqual("131st");
});

test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect( getOrdinalNumber(2) ).toEqual("2nd");
expect( getOrdinalNumber(22) ).toEqual("22nd");
expect( getOrdinalNumber(132) ).toEqual("132nd");
});
test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect( getOrdinalNumber(33) ).toEqual("33rd");
expect( getOrdinalNumber(133) ).toEqual("133rd");
});
test("append 'th' to numbers ending in 4, except those ending in 14", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect( getOrdinalNumber(24) ).toEqual("24th");
expect( getOrdinalNumber(134) ).toEqual("134th");
});
test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The tests on lines 11-26 are good.

Can you improve the tests on 27-34?

  • Why differentiate numbers ending in 4 from numbers that ending in 14?
  • What about numbers ending in other digits?
  • Why prepare a test just for 11?

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, I tried to improve the tests on line 27-34. also added a test for other numbers.
11 is exception as it ends with 1 but will get "th", so tested separately.

18 changes: 16 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
function repeat() {
return "hellohellohello";
function repeat(str , count) {
if (count < 0) {
throw new Error("Count must be a non-negative integer");
}
Comment on lines +2 to +4
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How would the caller distinguish the result of the following two function calls?

  1. repeat("Count must be a non-negative integer", 1)
  2. repeat("", -1)

Both function calls return the same value.

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, I see your point. I changed it so it throw error.

if (count === 0) {
return "";
}

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

return result;
}
console.log(repeat("hello", 2));


module.exports = repeat;
24 changes: 18 additions & 6 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Implement a function repeat
const repeat = require("./repeat");
// Given a target string str and a positive integer count,
// When the repeat function is called with these inputs,
// Then it should:

// case: repeat String:
// Given a target string str and a positive integer count,
// When the repeat function is called with these inputs,
Expand All @@ -20,13 +16,29 @@ 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 = "world";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("world");
});
// 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";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).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 for negative count", () => {
const str = "error";
const count = -2;

expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer");
});