Skip to content

Commit 9dcdbd9

Browse files
author
lintsang
committed
Revised all 2-practice-tdd exercises
1 parent 154190c commit 9dcdbd9

6 files changed

Lines changed: 47 additions & 64 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,7 +1,7 @@
11
function countChar(stringOfCharacters, findCharacter) {
22
let countTime = 0;
33
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.
4+
if (stringOfCharacters[i] == findCharacter) {//take each character out from string to compare with the given character. If true, then add one to countTime.
55
countTime = countTime + 1;
66
}
77
}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@ test("should count multiple occurrences of a character", () => {
1515
const char = "a";
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
18-
});
1918

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-
});
19+
const str2 = "4444444444444444444";
20+
const char2 = "4";
21+
const count2 = countChar(str2, char2);
22+
expect(count2).toEqual(19);
2623

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-
});
24+
const str3 = "aaabcde";
25+
const char3 = "c";
26+
const count3 = countChar(str3, char3);
27+
expect(count3).toEqual(1);
3328

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);
29+
const str4 = "a2b3c43";
30+
const char4 = "3";
31+
const count4 = countChar(str4, char4);
32+
expect(count4).toEqual(2);
3933
});
4034

4135
// Scenario: No Occurrences

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

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
function getOrdinalNumber(num) {
2-
let onesDigit;
3-
2+
let onesDigit = Number(String(num).slice(-1)); //extract the figure at the tens digit, and convert it into a number.
3+
let tensDigit;
44
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) {
5+
tensDigit = Number(String(num).slice(-2, -1)); //extract the figure at the tens digit, and convert it into a number.
6+
}
7+
if (tensDigit === 1) {
8+
return num + "th";
9+
} else {
10+
if (onesDigit === 1) {
3111
return num + "st";
32-
console.log("Yeah youre in");
33-
} else if (onesDigit == 2) {
12+
} else if (onesDigit === 2) {
3413
return num + "nd";
35-
} else if (onesDigit == 3) {
14+
} else if (onesDigit === 3) {
3615
return num + "rd";
3716
} else {
3817
return num + "th";

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ const getOrdinalNumber = require("./get-ordinal-number");
1313
// Case 1: Numbers ending with 1 (but not 11)
1414
// When the number ends with 1, except those ending with 11,
1515
// Then the function should return a string by appending "st" to the number.
16-
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
16+
test("should append 'st' for numbers ending with 1, except those having figure 1 in tens digit", () => {
1717
expect(getOrdinalNumber(1)).toEqual("1st");
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
2121

22-
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
22+
test("should append 'nd' for numbers ending with 2, except those having figure 1 in tens digit", () => {
2323
expect(getOrdinalNumber(2)).toEqual("2nd");
2424
expect(getOrdinalNumber(22)).toEqual("22nd");
2525
expect(getOrdinalNumber(132)).toEqual("132nd");
2626
});
2727

28-
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
28+
test("should append 'rd' for numbers ending with 3, except those having figure 1 in tens digit", () => {
2929
expect(getOrdinalNumber(3)).toEqual("3rd");
3030
expect(getOrdinalNumber(23)).toEqual("23rd");
3131
expect(getOrdinalNumber(133)).toEqual("133rd");
3232
});
3333

34-
test("should append 'th' for other numbers without ending with 1,2 or 3, except those ending with 1 in tens digit", () => {
34+
test("should append 'th' for other numbers ending with 1,2 or 3, except those having figure 1 in tens digit", () => {
3535
expect(getOrdinalNumber(11)).toEqual("11th");
3636
expect(getOrdinalNumber(12)).toEqual("12th");
3737
expect(getOrdinalNumber(13)).toEqual("13th");
@@ -40,3 +40,16 @@ test("should append 'th' for other numbers without ending with 1,2 or 3, except
4040
expect(getOrdinalNumber(313)).toEqual("313th");
4141
expect(getOrdinalNumber(1113)).toEqual("1113th");
4242
});
43+
44+
test("should append 'th' for other numbers ending other than 1,2 or 3", () => {
45+
expect(getOrdinalNumber(5)).toEqual("5th");
46+
expect(getOrdinalNumber(8)).toEqual("8th");
47+
expect(getOrdinalNumber(9)).toEqual("9th");
48+
expect(getOrdinalNumber(10)).toEqual("10th");
49+
expect(getOrdinalNumber(57)).toEqual("57th");
50+
expect(getOrdinalNumber(79)).toEqual("79th");
51+
expect(getOrdinalNumber(94)).toEqual("94th");
52+
expect(getOrdinalNumber(266)).toEqual("266th");
53+
expect(getOrdinalNumber(465)).toEqual("465th");
54+
expect(getOrdinalNumber(1070)).toEqual("1070th");
55+
});

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ 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.
44
let strOutput = "";
5-
if (count > 0){
5+
if (count >= 0){
66
for (let i=0 ; i<count ; i++){
77
strOutput = strOutput + str;
88
}
99
return strOutput;
10-
} else if (count == 0){
11-
return "";
1210
}else{
13-
return "error";
11+
throw new Error("Negative count is not valid");
1412
}
1513
}
1614

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ test("should return empty string as the count is zero", () => {
4242
// Given a target string `str` and a negative integer `count`,
4343
// When the repeatStr function is called with these inputs,
4444
// 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-
});
45+
test("should throw an error as a negative count is not valid", () => {
46+
expect(() => {
47+
repeatStr("evening", -1);
48+
}).toThrow();
49+
});

0 commit comments

Comments
 (0)