Skip to content

Commit 4e088d1

Browse files
committed
Complete practice-tdd assignments cleanly from main
1 parent b31a586 commit 4e088d1

6 files changed

Lines changed: 73 additions & 6 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+
let count = 0;
3+
4+
for (let char of stringOfCharacters) {
5+
if (char === findCharacter) {
6+
count++;
7+
}
8+
}
9+
return count;
310
}
411

512
module.exports = countChar;

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

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

20-
// Scenario: No Occurrences
20+
//Scenario: No Occurrences
21+
test("should return 0 when the character does not exist in the string", () => {
22+
const str = "hello";
23+
const char = "z";
24+
const count = countChar(str, char);
25+
expect(count).toEqual(0);
26+
});
27+
2128
// Given the input string `str`,
2229
// And a character `char` that does not exist within `str`.
2330
// When the function is called with these inputs,
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";
3-
}
2+
let lastDigit = num % 10;
3+
let lastTwoDigits = num % 100;
4+
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
5+
return `${num}th`;
6+
}
7+
if (lastDigit === 1) {
8+
return `${num}st`;
9+
}
10+
else if (lastDigit === 2) {
11+
return `${num}nd`;
12+
}
13+
else if (lastDigit === 3) {
14+
return `${num}rd`;
15+
}
16+
else {
17+
return `${num}th`;
18+
}
19+
}
420

521
module.exports = getOrdinalNumber;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ 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("converts 1 to an ordinal number", () => {
22+
expect(getOrdinalNumber(1)).toEqual("1st");
23+
expect(getOrdinalNumber(11)).toEqual("11th");
24+
});
25+
test("converts numbers to ordinal number", () => {
26+
expect(getOrdinalNumber(21)).toEqual("21st");
27+
expect(getOrdinalNumber(33)).toEqual("33rd");
28+
expect(getOrdinalNumber(13)).toEqual("13th");
29+
});
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
function repeatStr() {
1+
function repeatStr(str, count) {
2+
let result = "";
3+
for (let i =0; i<count;i++){
4+
result +=str;
5+
}
6+
27
// 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).
38
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
9+
return result;
510
}
11+
console.log(repeatStr("hello",3))
612

713
module.exports = repeatStr;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,35 @@ 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+
// Case: handle count of 1:
24+
test("should return the original string unchanged when count is 1", () => {
25+
const str = "apple";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("apple");
29+
});
30+
2331

2432
// Case: Handle count of 0:
2533
// Given a target string `str` and a `count` equal to 0,
2634
// When the repeatStr function is called with these inputs,
2735
// Then it should return an empty string.
2836

37+
test("should return an empty string when count is 0", () => {
38+
const str = "world";
39+
const count = 0;
40+
const repeatedStr = repeatStr(str, count);
41+
expect(repeatedStr).toEqual("");
42+
});
43+
2944
// Case: Handle negative count:
3045
// Given a target string `str` and a negative integer `count`,
3146
// When the repeatStr function is called with these inputs,
3247
// Then it should throw an error, as negative counts are not valid.
48+
49+
test("should throw an error when count is a negative integer", () => {
50+
const str = "test";
51+
const count = -5;
52+
expect(() => repeatStr(str, count)).toThrow();
53+
});
54+

0 commit comments

Comments
 (0)