Skip to content

Commit be7308e

Browse files
committed
Fixed some errors
1 parent effcc4b commit be7308e

File tree

4 files changed

+45
-59
lines changed

4 files changed

+45
-59
lines changed

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
// Ensure valid inputs
3-
if (
4-
typeof stringOfCharacters !== "string" ||
5-
typeof findCharacter !== "string"
6-
) {
7-
return 0;
8-
}
9-
10-
// Count occurrences
11-
let count = 0;
12-
for (let char of stringOfCharacters) {
13-
if (char === findCharacter) {
14-
count++;
2+
let total = 0;
3+
for (let i=0; i < stringOfCharacters.length; i++){
4+
if (findCharacter == stringOfCharacters[i]){
5+
total++;
156
}
16-
}
17-
18-
return count;
7+
}
8+
return total;
199
}
2010

2111
module.exports = countChar;
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
function getOrdinalNumber(num) {
2-
// Ensure the input is a valid number
3-
if (typeof num !== "number" || isNaN(num)) {
4-
return "";
5-
}
6-
7-
const lastTwoDigits = num % 100;
8-
const lastDigit = num % 10;
2+
3+
let result;
94

10-
// Handle special cases: 11th, 12th, 13th
11-
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
12-
return `${num}th`;
5+
if (num % 100 == 11 || num % 100 == 12 || num % 100 == 13){
6+
result = num.toString() + "th";
137
}
14-
15-
// Handle normal ordinal endings
16-
switch (lastDigit) {
17-
case 1:
18-
return `${num}st`;
19-
case 2:
20-
return `${num}nd`;
21-
case 3:
22-
return `${num}rd`;
23-
default:
24-
return `${num}th`;
8+
else if (num % 10 == 1){
9+
result = num.toString() +"st";
10+
}
11+
else if (num % 10 == 2){
12+
result = num.toString() + "nd";
2513
}
26-
}
14+
else if (num % 10 == 3){
15+
result = num.toString() + "rd";
16+
}
17+
else {
18+
result = num.toString() + "th";
19+
}
20+
21+
return result
22+
}
2723

2824
module.exports = getOrdinalNumber;

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
function repeat(str, count) {
2-
// Validate inputs
3-
if (typeof str !== "string") {
4-
throw new Error("First argument must be a string");
5-
}
62

7-
if (typeof count !== "number" || count < 0) {
8-
throw new Error("Count must be a non-negative number");
9-
}
3+
if (count < 0) {
4+
return null;
5+
} else if (count == 0) {
6+
return "";
7+
}
8+
let result = "";
9+
for (let i = 0; i < count; i++) {
10+
result += str;
11+
}
12+
return result;
1013

11-
// Repeat string count times
12-
return str.repeat(count);
1314
}
1415

1516
module.exports = repeat;

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ test("should repeat the string count times", () => {
2020
// Given a target string str and a count equal to 1,
2121
// When the repeat function is called with these inputs,
2222
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
23-
test("should return the original string when count is 1", () => {
24-
const str = "hi";
23+
test("handle Count of 1", () => {
24+
const str = "hello";
2525
const count = 1;
2626
const repeatedStr = repeat(str, count);
27-
expect(repeatedStr).toEqual("hi");
27+
expect(repeatedStr).toEqual("hello");
2828
});
2929

3030
// case: Handle Count of 0:
3131
// Given a target string str and a count equal to 0,
3232
// When the repeat function is called with these inputs,
3333
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
34-
test("should return an empty string when count is 0", () => {
35-
const str = "test";
34+
test("handle Count of 0", () => {
35+
const str = "hello";
3636
const count = 0;
3737
const repeatedStr = repeat(str, count);
3838
expect(repeatedStr).toEqual("");
@@ -42,10 +42,9 @@ test("should return an empty string when count is 0", () => {
4242
// Given a target string str and a negative integer count,
4343
// When the repeat function is called with these inputs,
4444
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45-
test("should throw an error when count is negative", () => {
46-
const str = "error";
47-
const count = -2;
48-
expect(() => repeat(str, count)).toThrow(
49-
"Count must be a non-negative number"
50-
);
51-
});
45+
test("Negative Count", () => {
46+
const str = "hello";
47+
const count = -1;
48+
const repeatedStr = repeat(str, count);
49+
expect(repeatedStr).toEqual(null);
50+
});

0 commit comments

Comments
 (0)