Skip to content

Commit b470944

Browse files
committed
Sprint 3 all tasks under Practice-tdd is done
1 parent b31a586 commit b470944

6 files changed

Lines changed: 91 additions & 7 deletions

File tree

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

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

513
module.exports = countChar;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ 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 = "Code your Future";
22+
const char = "u";
23+
const count = countChar(str, char);
24+
expect(count).toEqual(3);
25+
});
26+
27+
test("should count multiple occurrences of a character", () => {
28+
const str = "Introduction to Programming";
29+
const char = "o";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(4);
32+
});
33+
34+
test("should count multiple occurrences of a character", () => {
35+
const str = "Introduction to Javascript";
36+
const char = "i";
37+
const count = countChar(str, char);
38+
expect(count).toEqual(2);
39+
});
2040
// Scenario: No Occurrences
2141
// Given the input string `str`,
2242
// And a character `char` that does not exist within `str`.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const s = ["th", "st", "nd", "rd"];
3+
const lastDigits = num % 100;
4+
// Uses the index 1, 2, or 3 for st/nd/rd, defaults to 0 (th)
5+
return num + (s[(lastDigits - 20) % 10] || s[lastDigits] || s[0]);
36
}
47

58
module.exports = getOrdinalNumber;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,27 @@ 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 numbers ending with 4-9, 0, and teens (11-13)", () => {
35+
expect(getOrdinalNumber(11)).toEqual("11th");
36+
expect(getOrdinalNumber(12)).toEqual("12th");
37+
expect(getOrdinalNumber(13)).toEqual("13th");
38+
expect(getOrdinalNumber(4)).toEqual("4th");
39+
expect(getOrdinalNumber(5)).toEqual("5th");
40+
expect(getOrdinalNumber(10)).toEqual("10th");
41+
expect(getOrdinalNumber(70)).toEqual("70th");
42+
expect(getOrdinalNumber(40)).toEqual("40th");
43+
});
44+
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
function repeatStr() {
2-
// 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).
3-
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
let result = "";
3+
for (let i = 0; i < count; i++) {
4+
result += str;
5+
}
6+
return result;
57
}
6-
78
module.exports = repeatStr;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ test("should repeat the string count times", () => {
1616
expect(repeatedStr).toEqual("hellohellohello");
1717
});
1818

19+
test("should repeat the string count times", () => {
20+
const str = "CYF";
21+
const count = 3;
22+
const repeatedStr = repeatStr(str, count);
23+
expect(repeatedStr).toEqual("CYFCYFCYF");
24+
});
25+
26+
test("should repeat the string count times", () => {
27+
const str = "Code Your Future";
28+
const count = 1;
29+
const repeatedStr = repeatStr(str, count);
30+
expect(repeatedStr).toEqual("Code Your Future");
31+
});
32+
33+
test("should repeat the string count times", () => {
34+
const str = "hello";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("");
38+
});
39+
40+
test("should repeat the string count times", () => {
41+
const str = "ITP";
42+
const count = 5;
43+
const repeatedStr = repeatStr(str, count);
44+
expect(repeatedStr).toEqual("ITPITPITPITPITP");
45+
});
46+
1947
// Case: handle count of 1:
2048
// Given a target string `str` and a `count` equal to 1,
2149
// When the repeatStr function is called with these inputs,

0 commit comments

Comments
 (0)