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
4 changes: 3 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
const totalChar = stringOfCharacters.split(findCharacter);

return totalChar.length - 1;
}

module.exports = countChar;
9 changes: 9 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ 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
// Given the input string str,
// 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 check for no occurrences of a character", () => {
const str = "abcdef";
const char = "g";
const count = countChar(str, char);
expect(count).toEqual(0);
});
30 changes: 29 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,33 @@
function getOrdinalNumber(num) {
return "1st";
if (Number.isInteger(num) && num >= 0) {
const lastDigit = num % 10;

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

return `${num}st`;
} else if (lastDigit === 2) {
if (num > 10) {
if (num % 100 === 12) {
return `${num}th`;
}
}
return `${num}nd`;
} else if (lastDigit === 3) {
if (num > 10) {
if (num % 100 === 13) {
return `${num}th`;
}
}
return `${num}rd`;
}

return `${num}th`;
} else {
throw new Error("wrong input used please enter an integer from 0 upwards");
}
}

module.exports = getOrdinalNumber;
45 changes: 44 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,49 @@ 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 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 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 append 'th' to all other numbers which do not end in 1,2,3,11,12 or 13", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(9)).toEqual("9th");
expect(getOrdinalNumber(211)).toEqual("211th");
expect(getOrdinalNumber(1013)).toEqual("1013th");
expect(getOrdinalNumber(17)).toEqual("17th");
});

test("should throw an error if the input anything other than a positive integer", () => {
expect(() => getOrdinalNumber(1.09)).toThrow(
"wrong input used please enter an integer from 0 upwards"
);
expect(() => getOrdinalNumber("thirteen")).toThrow(
"wrong input used please enter an integer from 0 upwards"
);
expect(() => getOrdinalNumber(0.9)).toThrow(
"wrong input used please enter an integer from 0 upwards"
);
expect(() => getOrdinalNumber(-10)).toThrow(
"wrong input used please enter an integer from 0 upwards"
);
});
Loading