Skip to content
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 count = 0;

for (const i of stringOfCharacters) {
if (i === findCharacter) {
count++;
}
}

return count;
}

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 `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.
test("should not find any occurrences", () => {
const str = "aaaaaaaa";
const char = "b";

const count = countChar(str, char);
expect(count).toEqual(0);
});
26 changes: 25 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,29 @@
function getOrdinalNumber(num) {
return "1st";
if (typeof num !== "number" || Number.isNaN(num) || !Number.isInteger(num)) {
throw new Error(`Invalid type for ${num}, expected Number`);
}

const snum = String(num); // num as a string
const thList = ["11", "12", "13"]; // list of numbers that should end up with th as a suffix

const lastCharIndex = snum.length - 1;
const lastOne = snum[lastCharIndex];
const lastTwo = snum.slice(lastCharIndex - 1);

if (thList.includes(lastTwo)) {
return snum + "th";
}

switch (lastOne) {
case "1":
return snum + "st";
case "2":
return snum + "nd";
case "3":
return snum + "rd";
default:
return snum + "th";
}
}

module.exports = getOrdinalNumber;
63 changes: 63 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,66 @@ 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 0, 4-9, 10, 11, 12, 13", () => {
// 0
expect(getOrdinalNumber(0)).toEqual("0th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(100)).toEqual("100th");
// 4
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(24)).toEqual("24th");
expect(getOrdinalNumber(134)).toEqual("134th");

// 5
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(25)).toEqual("25th");
expect(getOrdinalNumber(135)).toEqual("135th");

// 6
expect(getOrdinalNumber(6)).toEqual("6th");
expect(getOrdinalNumber(26)).toEqual("26th");
expect(getOrdinalNumber(136)).toEqual("136th");

// 7
expect(getOrdinalNumber(7)).toEqual("7th");
expect(getOrdinalNumber(27)).toEqual("27th");
expect(getOrdinalNumber(137)).toEqual("137th");

// 8
expect(getOrdinalNumber(8)).toEqual("8th");
expect(getOrdinalNumber(28)).toEqual("28th");
expect(getOrdinalNumber(138)).toEqual("138th");

// 9
expect(getOrdinalNumber(9)).toEqual("9th");
expect(getOrdinalNumber(29)).toEqual("29th");
expect(getOrdinalNumber(139)).toEqual("139th");

// 10
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(100)).toEqual("100th");

// 11, 12, 13
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});

// edge cases
test("should throw an error when given a wrong input type", () => {
expect(() => getOrdinalNumber("testing string")).toThrow("Invalid type for ");
expect(() => getOrdinalNumber("")).toThrow();
expect(() => getOrdinalNumber(null)).toThrow();
expect(() => getOrdinalNumber(undefined)).toThrow();
});
14 changes: 12 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
function repeatStr() {
function repeatStr(str, count) {
// 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";
if (count < 0) {
throw new Error("Expected a positive integer");
}

let out = "";

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

return out;
}

module.exports = repeatStr;
19 changes: 19 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,31 @@ test("should repeat the string count times", () => {
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.

test("Should return the original string with no repetitions", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});

// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.
test("Should return an empty string", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.

test("Should throw an error, as negative counts are not valid", () => {
const str = "hello";
const count = -1;
expect(() => repeatStr(str, count)).toThrow("Expected a positive integer");
});
Loading