Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 8 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
function countChar(stringOfCharacters, findCharacter) {
let pattern = new RegExp(findCharacter, "g");
const totalChar = stringOfCharacters.split(findCharacter);

let matchingChars = stringOfCharacters.match(pattern);

if (matchingChars === null) {
return 0;
}

return matchingChars.length;
return totalChar.length - 1;
}

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

// Scenario: No Occurrences
Expand Down
37 changes: 28 additions & 9 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
function getOrdinalNumber(num) {
if (num === 1) {
return "1st";
} else if (num === 2) {
return "2nd";
} else if (num === 3) {
console.log(num);
return "3rd";
} else if (num > 3 || num < 21) {
return `${num}th`;
const lastDigit = num.toString()[num.toString().length - 1];
Copy link
Contributor

Choose a reason for hiding this comment

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

Could consider using the more efficient approach involving the % operator to extract the last digit and the last two digits from a number directly.

Copy link
Author

Choose a reason for hiding this comment

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

used the % to have less code and less function calls


if (lastDigit === "1") {
if (num === 11) {
return `${num}th`;
}

return `${num}st`;
} else if (lastDigit === "2") {
if (num.toString().length > 1) {
const last2Digits = num.toString().slice(-2);

if (last2Digits === "12") {
return `${num}th`;
}
}
return `${num}nd`;
} else if (lastDigit === "3") {
if (num.toString().length > 1) {
const last2Digits = num.toString().slice(-2);

if (last2Digits === "13") {
return `${num}th`;
}
}
return `${num}rd`;
}

return `${num}th`;
}

module.exports = getOrdinalNumber;
24 changes: 20 additions & 4 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,34 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
test("should append 'st' to numbers with 1 at the end except for those ending with 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(101)).toEqual("101st");
expect(getOrdinalNumber(151)).toEqual("151st");
expect(getOrdinalNumber(2061)).toEqual("2061st");
});

test("should return '2nd' for 2", () => {
test("should append 'nd' to numbers with 2 at the end except for those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(342)).toEqual("342nd");
expect(getOrdinalNumber(592)).toEqual("592nd");
expect(getOrdinalNumber(1972)).toEqual("1972nd");
});

test("should return '3rd' for 3", () => {
test("should append 'rd' to numbers with 3 at the end except for those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(353)).toEqual("353rd");
expect(getOrdinalNumber(93)).toEqual("93rd");
expect(getOrdinalNumber(783)).toEqual("783rd");
});

test("should return any number between 4 and 20 with a suffix of 'th' at end of number", () => {
test("should append 'th' to all other numbers which do not end in 1,2,3,11,12 or 13", () => {
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(212)).toEqual("212th");
expect(getOrdinalNumber(113)).toEqual("113th");
expect(getOrdinalNumber(17)).toEqual("17th");
});
Comment on lines 11 to 41
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests are quite comprehensive. If you use this test script to test your implementation, you can discover some bug in your code.

Copy link
Author

Choose a reason for hiding this comment

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

added code to catch negative and non-integer values

8 changes: 6 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ function repeat(str, count) {
if (count === 0) {
return newStr;
} else if (count === 1) {
return (newStr += str);
return str;
} else if (count > 1) {
for (let i = 0; i < count; i++) {
newStr += str;
}
return newStr;
} else {
return "Error invalid count used, please use integers from 0 upwards.";
throw new Error(
"Error invalid count used, please use integers from 0 upwards."
);
//return "Error invalid count used, please use integers from 0 upwards.";
}
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

  • What's the purpose of the statement on line 19? When do you expect it to be executed?

  • You can also consider structuring your code in the following way:

  if (code < 0)
    throw ...

  if (code == 0) return "";

  if (code == 1) return ...;

  let newStr = "";
  ...
  return newStr;

or (if you don't mind trading performance for simpler code)

  if (count < 0)
    throw ...;

  // this code will produce the correct result for any count >= 0 anyway.
  let newStr = "";
  ...
  return newstr;

Copy link
Author

Choose a reason for hiding this comment

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

refactored the code to reduce the number of if else statements.

}

module.exports = repeat;
6 changes: 4 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ test("should return an empty string", () => {
test("should throw an error for invalid count", () => {
const str = "hello";
const count = -4;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual(
function repeatedStr() {
repeat(str, count);
}
expect(repeatedStr).toThrow(
"Error invalid count used, please use integers from 0 upwards."
);
});