Skip to content

Commit 16e1c56

Browse files
fixed the tdd tests
1 parent 2d01d60 commit 16e1c56

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
if (
3+
typeof stringOfCharacters !== "string" ||
4+
typeof findCharacter !== "string"
5+
) {
6+
return 0;
7+
}
8+
let count = 0;
9+
for (let i = 0; i < stringOfCharacters.length; i++) {
10+
if (stringOfCharacters[i] === findCharacter) {
11+
count++;
12+
}
13+
}
14+
return count;
315
}
4-
16+
console.log(countChar("aaaaa", "a")); // should return 5
17+
console.log(countChar("bravo", "u")); // should return 0
18+
console.log(countChar("", "a")); // should return 0
19+
console.log(countChar("1-2-3-4-5-", "-")); // should return 5
20+
console.log(countChar("AaAa", "A")); // should return 2
521
module.exports = countChar;
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if (typeof num !== "number" || !Number.isInteger(num) || num < 0) {
3+
throw new Error("Input must be a non-negative integer");
4+
}
5+
6+
const lastTwo = num % 100;
7+
const lastDigit = num % 10;
8+
9+
// Special cases: 11th, 12th, 13th
10+
if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
11+
return `${num}th`;
12+
}
13+
14+
// Normal cases
15+
if (lastDigit === 1) return `${num}st`;
16+
if (lastDigit === 2) return `${num}nd`;
17+
if (lastDigit === 3) return `${num}rd`;
18+
19+
return `${num}th`;
320
}
421

522
module.exports = getOrdinalNumber;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ test("should append 'th' for numbers ending with 11, 12, 13", () => {
4242
});
4343

4444
// Case 5: Numbers ending with larger numbers of 111, 121, 131
45-
test("should append 'th' for numbers ending with 111, 121, 131", () => {
45+
test("should append 'th' for numbers ending with 111, 112, 113", () => {
4646
expect(getOrdinalNumber(111)).toEqual("111th");
47-
expect(getOrdinalNumber(121)).toEqual("121th");
48-
expect(getOrdinalNumber(131)).toEqual("131th");
47+
expect(getOrdinalNumber(112)).toEqual("112th");
48+
expect(getOrdinalNumber(113)).toEqual("113th");
4949
});

0 commit comments

Comments
 (0)