Skip to content

Commit 869c92a

Browse files
Sprint 3 Practice TDD exercises
1 parent 3372770 commit 869c92a

File tree

6 files changed

+110
-7
lines changed

6 files changed

+110
-7
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
1+
function countChar(str, char) {
2+
let count = 0;
3+
for (let i = 0; i < str.length; i++)
4+
if(str[i] === char)
5+
{
6+
count++;
7+
}
8+
return count;
39
}
410

11+
512
module.exports = countChar;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ test("should count multiple occurrences of a character", () => {
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
1818
});
19+
test("should count no occurrences of a character",()=>{
20+
const str = "hello";
21+
const char = "a";
22+
const count = countChar(str, char);
23+
expect(count).toEqual(0);
24+
});
1925

2026
// Scenario: No Occurrences
2127
// Given the input string `str`,
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( num%10 === 1 && num%100 !== 11)
3+
{
4+
return `${num}st`;
5+
}
6+
else if (num%10 === 2 && num%100 !== 12)
7+
{
8+
return `${num}nd`;
9+
}
10+
else if(num%10===3 && num%100 !==13)
11+
{
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,43 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
test("should append 'th' for numbers ending with 11", ()=>{
22+
expect(getOrdinalNumber(11)).toEqual("11th");
23+
expect(getOrdinalNumber(111)).toEqual("111th");
24+
expect(getOrdinalNumber(211)).toEqual("211th");
25+
expect(getOrdinalNumber(311)).toEqual("311th");
26+
});
27+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
28+
expect(getOrdinalNumber(2)).toEqual("2nd");
29+
expect(getOrdinalNumber(22)).toEqual("22nd");
30+
expect(getOrdinalNumber(132)).toEqual("132nd");
31+
});
32+
test("should append 'th' for numbers ending with 12", () => {
33+
expect(getOrdinalNumber(12)).toEqual("12th");
34+
expect(getOrdinalNumber(112)).toEqual("112th");
35+
expect(getOrdinalNumber(212)).toEqual("212th");
36+
});
37+
38+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
39+
expect(getOrdinalNumber(3)).toEqual("3rd");
40+
expect(getOrdinalNumber(33)).toEqual("33rd");
41+
expect(getOrdinalNumber(133)).toEqual("133rd");
42+
});
43+
test("should append 'th' for numbers ending with 13", () => {
44+
expect(getOrdinalNumber(13)).toEqual("13th");
45+
expect(getOrdinalNumber(113)).toEqual("113th");
46+
expect(getOrdinalNumber(213)).toEqual("213th");
47+
});
48+
test("should append 'th' for numbers ending with 4", () => {
49+
expect(getOrdinalNumber(4)).toEqual("4th");
50+
expect(getOrdinalNumber(14)).toEqual("14th");
51+
expect(getOrdinalNumber(134)).toEqual("134th");
52+
});
53+
54+
55+
56+
57+
58+
59+
60+
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count <0)
3+
{
4+
throw new Error("negative counts are not valid");
5+
}
6+
else if(count === 1)
7+
{
8+
return str;
9+
}
10+
else if (count === 0)
11+
{
12+
return "";
13+
}
14+
else
15+
{
16+
let result ="";
17+
for (let i =0; i<count;i++)
18+
{
19+
20+
result =result+str;
21+
}
22+
return result;
23+
}
324
}
425

526
module.exports = repeatStr;

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@ const repeatStr = require("./repeat-str");
1212
test("should repeat the string count times", () => {
1313
const str = "hello";
1414
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
15+
expect(repeatStr(str,count)).toEqual("hellohellohello");
16+
});
17+
test(" should return original 'str' without repetition", ()=>{
18+
const str = "hello";
19+
const count = 1;
20+
expect(repeatStr(str, count)).toEqual("hello");
21+
});
22+
test("should return empty string", ()=>{
23+
const str = "hello";
24+
const count = 0;
25+
expect(repeatStr(str, count)).toEqual("");
26+
27+
});
28+
test("should throw negative counts are not valid Error", ()=>{
29+
const str = "hello";
30+
const count = -1;
31+
expect(()=> repeatStr(str, count)).toThrow();
1732
});
1833

1934
// Case: handle count of 1:

0 commit comments

Comments
 (0)