Skip to content

Commit 639af4c

Browse files
committed
Implement functions to repeat strings, get ordinal numbers, and count; pass all test cases
1 parent 3372770 commit 639af4c

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
// our function;
12
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
3+
if (!stringOfCharacters || !findCharacter) {
4+
return 0;
5+
}
6+
let numberOfChar = 0;
7+
for (let wantedChar of stringOfCharacters) {
8+
if (wantedChar === findCharacter) {
9+
numberOfChar++;
10+
}
11+
if (numberOfChar === 0) {
12+
return `0 ${findCharacter} found.`
13+
}
14+
}
15+
return numberOfChar;
316
}
417

518
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 if there is not a character occurrence", () => {
26+
const str = "aaaaa";
27+
const char = "b";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(`0 ${char} found.`);
30+
});
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
// lets develop our function.
12
function getOrdinalNumber(num) {
2-
return "1st";
3+
4+
const lastDigit = num % 10; // ==> 111 % 10 = 11.1 last digit is 1.
5+
const lastTwoDigits = num % 100; // 111 % 100 = 111 last two digits 11.
6+
7+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13){
8+
return `${num}th`;// ==> finds the last two digits to equal (11,12,13)
9+
}
10+
if(lastDigit == 1){
11+
return `${num}st`;
12+
}else if (lastDigit == 2){
13+
return `${num}nd`;
14+
}else if (lastDigit == 3){
15+
return `${num}rd`;
16+
} else return `${num}th`;
317
}
418

519
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+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
22+
expect(getOrdinalNumber(2)).toEqual("2nd");
23+
expect(getOrdinalNumber(22)).toEqual("22nd");
24+
expect(getOrdinalNumber(222)).toEqual("222nd");
25+
});
26+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
27+
expect(getOrdinalNumber(3)).toEqual("3rd");
28+
expect(getOrdinalNumber(33)).toEqual("33rd");
29+
expect(getOrdinalNumber(333)).toEqual("333rd");
30+
});
31+
test("should append 'th' for numbers ending with 4 and more than 4", () => {
32+
expect(getOrdinalNumber(4)).toEqual("4th");
33+
expect(getOrdinalNumber(44)).toEqual("44th");
34+
expect(getOrdinalNumber(444)).toEqual("444th");
35+
expect(getOrdinalNumber(5)).toEqual("5th");
36+
expect(getOrdinalNumber(10)).toEqual("10th");
37+
expect(getOrdinalNumber(100)).toEqual("100th");
38+
expect(getOrdinalNumber(105)).toEqual("105th");
39+
});
40+
test("should append 'th' for numbers ending with 11, 12 and 13", () => {
41+
expect(getOrdinalNumber(11)).toEqual("11th");
42+
expect(getOrdinalNumber(12)).toEqual("12th");
43+
expect(getOrdinalNumber(13)).toEqual("13th");
44+
});
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0)
3+
throw new Error("Negative counts are not valid");
4+
if (count === 0) return "";
5+
else return str.repeat(count);
36
}
47

58
module.exports = repeatStr;

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@ const repeatStr = require("./repeat-str");
1010
// Then it should return a string that contains the original `str` repeated `count` times.
1111

1212
test("should repeat the string count times", () => {
13-
const str = "hello";
14-
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
13+
expect(repeatStr("Love", 3)).toEqual("LoveLoveLove");
1714
});
1815

1916
// Case: handle count of 1:
2017
// Given a target string `str` and a `count` equal to 1,
2118
// When the repeatStr function is called with these inputs,
2219
// Then it should return the original `str` without repetition.
23-
20+
test("should print the original string", () => {
21+
expect(repeatStr("Love", 1)).toEqual("Love");
22+
});
2423
// Case: Handle count of 0:
2524
// Given a target string `str` and a `count` equal to 0,
2625
// When the repeatStr function is called with these inputs,
2726
// Then it should return an empty string.
28-
27+
test("should return an empty string", () => {
28+
expect(repeatStr("Love", 0)).toEqual("");
29+
});
2930
// Case: Handle negative count:
3031
// Given a target string `str` and a negative integer `count`,
3132
// When the repeatStr function is called with these inputs,
3233
// Then it should throw an error, as negative counts are not valid.
34+
test("should throw and error when count is a negative number", () => {
35+
expect(()=> repeatStr("Love", -3)).toThrow("Negative counts are not valid");
36+
});

0 commit comments

Comments
 (0)