Skip to content

Commit 3adb2de

Browse files
count and get ordinal number implemented and tests written
1 parent b31a586 commit 3adb2de

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

Sprint-3/2-practice-tdd/count.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
return stringOfCharacters.split(findCharacter).length-1
33
}
44

55
module.exports = countChar;

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,34 @@ test("should count multiple occurrences of a character", () => {
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
1818
});
19+
test("should count multiple occurrences of a character", () => {
20+
const str = "pneumonoultramicroscopicsilicovolcanoconoisis";
21+
const char = "o";
22+
const count = countChar(str, char);
23+
expect(count).toEqual(9);
24+
});
25+
test("should count multiple occurrences of a character", () => {
26+
const str = "pneumonoultramicroscopicsilicovolcanoconoisis";
27+
const char = "i";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(6);
30+
});
31+
1932

2033
// Scenario: No Occurrences
2134
// Given the input string `str`,
2235
// And a character `char` that does not exist within `str`.
2336
// When the function is called with these inputs,
2437
// Then it should return 0, indicating that no occurrences of `char` were found.
38+
test("should count multiple occurrences of a character", () => {
39+
const str = "interesting";
40+
const char = "b";
41+
const count = countChar(str, char);
42+
expect(count).toEqual(0);
43+
});
44+
test("should count multiple occurrences of a character", () => {
45+
const str = "future";
46+
const char = "m";
47+
const count = countChar(str, char);
48+
expect(count).toEqual(0);
49+
});
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if ([11, 12, 13].includes(num % 100)){
3+
return num + "th"
4+
}
5+
if (String(num).at(-1)=== "1"){
6+
return num + "st"
7+
}
8+
if (String(num).at(-1) === "2"){
9+
return num + "nd"
10+
}
11+
if (String(num).at(-1) === "3"){
12+
return num + "rd"
13+
}
14+
else{
15+
return num + "th"
16+
}
317
}
418

519
module.exports = getOrdinalNumber;

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,30 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1717
expect(getOrdinalNumber(1)).toEqual("1st");
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
20+
expect(getOrdinalNumber(1021)).toEqual("1021st");
2021
});
22+
//case 2; Numbers ending with 4,5,6,7,8,9 and 0
23+
test("should append 'th' for numbers ending with 4,5,6,7,8,9,0", () => {
24+
expect(getOrdinalNumber(104)).toEqual("104th");
25+
expect(getOrdinalNumber(2000)).toEqual("2000th");
26+
expect(getOrdinalNumber(167)).toEqual("167th");
27+
expect(getOrdinalNumber(19)).toEqual("19th");
28+
});
29+
//case 3; Numbers ending with 11,12,13
30+
test("should append 'th' for numbers ending with 11,12,13", () => {
31+
expect(getOrdinalNumber(1013313)).toEqual("1013313th");
32+
expect(getOrdinalNumber(2012)).toEqual("2012th");
33+
expect(getOrdinalNumber(16711)).toEqual("16711th");
34+
});
35+
//case 4;Numbers ending with 2
36+
test("should append 'nd' for numbers ending with 2", () => {
37+
expect(getOrdinalNumber(102)).toEqual("102nd");
38+
expect(getOrdinalNumber(2022)).toEqual("2022nd");
39+
expect(getOrdinalNumber(16732)).toEqual("16732nd");
40+
});
41+
//case 4;Numbers ending with 3
42+
test("should append 'nd' for numbers ending with 3", () => {
43+
expect(getOrdinalNumber(3)).toEqual("3rd");
44+
expect(getOrdinalNumber(2023)).toEqual("2023rd");
45+
expect(getOrdinalNumber(16733)).toEqual("16733rd");
46+
});

0 commit comments

Comments
 (0)