Skip to content

Commit 154190c

Browse files
author
lintsang
committed
2-practice-tdd exercise
1 parent b31a586 commit 154190c

6 files changed

Lines changed: 126 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let countTime = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters.slice(i, i + 1) == findCharacter) {//take each character out from string to compare with the given character. If true, then add one to countTime.
5+
countTime = countTime + 1;
6+
}
7+
}
8+
return countTime;
39
}
410

511
module.exports = countChar;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,36 @@ test("should count multiple occurrences of a character", () => {
1717
expect(count).toEqual(5);
1818
});
1919

20+
test("should count multiple occurrences of a character", () => {
21+
const str = "aaabcde";
22+
const char = "c";
23+
const count = countChar(str, char);
24+
expect(count).toEqual(1);
25+
});
26+
27+
test("should count multiple occurrences of a character", () => {
28+
const str = "a2b3c4";
29+
const char = "3";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(1);
32+
});
33+
34+
test("should count multiple occurrences of a character", () => {
35+
const str = "4444444444444444444";
36+
const char = "4";
37+
const count = countChar(str, char);
38+
expect(count).toEqual(19);
39+
});
40+
2041
// Scenario: No Occurrences
2142
// Given the input string `str`,
2243
// And a character `char` that does not exist within `str`.
2344
// When the function is called with these inputs,
2445
// Then it should return 0, indicating that no occurrences of `char` were found.
46+
47+
test("should count no occurrence of a character", () => {
48+
const str = "fghij";
49+
const char = "b";
50+
const count = countChar(str, char);
51+
expect(count).toEqual(0);
52+
});
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
let onesDigit;
3+
4+
if (String(num).length > 1) {
5+
//check if the number has more than 1 digit
6+
onesDigit = Number(String(num).slice(-1)); //extract the figure at the tens digit, and convert it into a number.
7+
const tensDigit = Number(String(num).slice(-2, -1)); //extract the figure at the tens digit, and convert it into a number.
8+
9+
if (onesDigit == 1) {
10+
if (tensDigit == 1) {
11+
return num + "th";
12+
} else {
13+
return num + "st";
14+
}
15+
} else if (onesDigit == 2) {
16+
if (tensDigit == 1) {
17+
return num + "th";
18+
}
19+
return num + "nd";
20+
} else if (onesDigit == 3) {
21+
if (tensDigit == 1) {
22+
return num + "th";
23+
}
24+
return num + "rd";
25+
} else {
26+
("th");
27+
}
28+
} else if (String(num).length == 1) {
29+
onesDigit = Number(String(num)[0]); //extract the figure at the tens digit, and convert it into a number.
30+
if (onesDigit == 1) {
31+
return num + "st";
32+
console.log("Yeah youre in");
33+
} else if (onesDigit == 2) {
34+
return num + "nd";
35+
} else if (onesDigit == 3) {
36+
return num + "rd";
37+
} else {
38+
return num + "th";
39+
}
40+
}
341
}
442

543
module.exports = getOrdinalNumber;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,25 @@ 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(132)).toEqual("132nd");
26+
});
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(133)).toEqual("133rd");
32+
});
33+
34+
test("should append 'th' for other numbers without ending with 1,2 or 3, except those ending with 1 in tens digit", () => {
35+
expect(getOrdinalNumber(11)).toEqual("11th");
36+
expect(getOrdinalNumber(12)).toEqual("12th");
37+
expect(getOrdinalNumber(13)).toEqual("13th");
38+
expect(getOrdinalNumber(113)).toEqual("113th");
39+
expect(getOrdinalNumber(213)).toEqual("213th");
40+
expect(getOrdinalNumber(313)).toEqual("313th");
41+
expect(getOrdinalNumber(1113)).toEqual("1113th");
42+
});
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 strOutput = "";
5+
if (count > 0){
6+
for (let i=0 ; i<count ; i++){
7+
strOutput = strOutput + str;
8+
}
9+
return strOutput;
10+
} else if (count == 0){
11+
return "";
12+
}else{
13+
return "error";
14+
}
515
}
616

717
module.exports = repeatStr;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,31 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23+
test("should return the string with no reptition", () => {
24+
const str = "morning";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("morning");
28+
});
2329

2430
// Case: Handle count of 0:
2531
// Given a target string `str` and a `count` equal to 0,
2632
// When the repeatStr function is called with these inputs,
2733
// Then it should return an empty string.
34+
test("should return empty string as the count is zero", () => {
35+
const str = "afternoon";
36+
const count = 0;
37+
const repeatedStr = repeatStr(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// Case: Handle negative count:
3042
// Given a target string `str` and a negative integer `count`,
3143
// When the repeatStr function is called with these inputs,
3244
// Then it should throw an error, as negative counts are not valid.
45+
test("should return the string with no reptition", () => {
46+
const str = "evening";
47+
const count = -1;
48+
const repeatedStr = repeatStr(str, count);
49+
expect(repeatedStr).toEqual("error");
50+
});

0 commit comments

Comments
 (0)