Skip to content

Commit 282f847

Browse files
committed
Attempt all TDD Tasks!
1 parent b31a586 commit 282f847

6 files changed

Lines changed: 84 additions & 25 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
const lengthOfStr = stringOfCharacters.length;
3+
let numOfOccurrence = 0;
4+
for(let i = 0; i< lengthOfStr ; i++){
5+
if(stringOfCharacters[i] == findCharacter){
6+
numOfOccurrence = numOfOccurrence + 1;
7+
}
8+
}
9+
return numOfOccurrence;
310
}
411

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
@@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
25+
test("should return 0 when the character does not exist in the string", () => {
26+
const str = "Hello world";
27+
const char = "a";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
let getStr = num.toString();
3+
let lastTwoDigit = getStr.slice(-2);
4+
let lastDigit = getStr.slice(-1);
5+
if(lastTwoDigit == "11" || lastTwoDigit == "12" || lastTwoDigit == "13"){
6+
return getStr + "th";
7+
}
8+
else if(lastDigit =="1"){
9+
return getStr + "st";
10+
}
11+
else if(lastDigit == "2"){
12+
return getStr + "nd";
13+
}
14+
else if(lastDigit == "3"){
15+
return getStr + "rd";
16+
}
17+
else{
18+
return getStr + "th";
19+
}
320
}
4-
521
module.exports = getOrdinalNumber;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ 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+
22+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(42)).toEqual("42nd");
26+
expect(getOrdinalNumber(102)).toEqual("102nd");
27+
});
28+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
29+
expect(getOrdinalNumber(3)).toEqual("3rd");
30+
expect(getOrdinalNumber(23)).toEqual("23rd");
31+
expect(getOrdinalNumber(103)).toEqual("103rd");
32+
});
33+
test("should append 'th' for numbers", () => {
34+
expect(getOrdinalNumber(4)).toEqual("4th");
35+
expect(getOrdinalNumber(10)).toEqual("10th");
36+
expect(getOrdinalNumber(13)).toEqual("13th");
37+
expect(getOrdinalNumber(100)).toEqual("100th");
38+
});
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
function repeatStr() {
1+
function repeatStr(str, count) {
22
// 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).
33
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
4+
let result="";
5+
if (count >=1){
6+
for(let i=1 ; i <= count ; i++){
7+
result= str + result;
8+
}
9+
return result;
10+
}
11+
else if (count == 0){
12+
return "";
13+
}
14+
throw new Error("invalid count")
515
}
616

717
module.exports = repeatStr;
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
// Implement a function repeatStr
22
const repeatStr = require("./repeat-str");
3-
// Given a target string `str` and a positive integer `count`,
4-
// When the repeatStr function is called with these inputs,
5-
// Then it should:
6-
7-
// Case: handle multiple repetitions:
8-
// Given a target string `str` and a positive integer `count` greater than 1,
9-
// When the repeatStr function is called with these inputs,
10-
// Then it should return a string that contains the original `str` repeated `count` times.
113

4+
// Case: handle multiple repetitions
125
test("should repeat the string count times", () => {
136
const str = "hello";
147
const count = 3;
158
const repeatedStr = repeatStr(str, count);
169
expect(repeatedStr).toEqual("hellohellohello");
1710
});
1811

19-
// Case: handle count of 1:
20-
// Given a target string `str` and a `count` equal to 1,
21-
// When the repeatStr function is called with these inputs,
22-
// Then it should return the original `str` without repetition.
12+
// Case: handle count of 1
13+
test("should return the original string when count is 1", () => {
14+
const str = "Maryam";
15+
const count = 1;
16+
const repeatedStr = repeatStr(str, count);
17+
expect(repeatedStr).toEqual(str);
18+
});
19+
20+
// Case: handle count of 0
21+
test("should return an empty string when count is 0", () => {
22+
const str = "Hello World!";
23+
const count = 0;
24+
const repeatedStr = repeatStr(str, count);
25+
expect(repeatedStr).toEqual("");
26+
});
2327

24-
// Case: Handle count of 0:
25-
// Given a target string `str` and a `count` equal to 0,
26-
// When the repeatStr function is called with these inputs,
27-
// Then it should return an empty string.
28+
// Case: handle negative count
29+
test("should throw an error when count is negative", () => {
30+
const str = "Hello World!";
31+
const count = -1;
2832

29-
// Case: Handle negative count:
30-
// Given a target string `str` and a negative integer `count`,
31-
// When the repeatStr function is called with these inputs,
32-
// Then it should throw an error, as negative counts are not valid.
33+
expect(() => repeatStr(str, count)).toThrow();
34+
});

0 commit comments

Comments
 (0)