Skip to content

Commit 6ca9d1f

Browse files
committed
Implement character counting and ordinal number formatting; update repeatStr function to avoid using String.prototype.repeat
1 parent a71143c commit 6ca9d1f

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

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

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

511
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+
3+
const lastDigit = num % 10;
4+
const lastTwoDigits = num % 100;
5+
6+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
7+
return `${num}th`;
8+
}
9+
10+
switch (lastDigit) {
11+
case 1:
12+
return `${num}st`;
13+
case 2:
14+
return `${num}nd`;
15+
case 3:
16+
return `${num}rd`;
17+
default:
18+
return `${num}th`;
19+
}
320
}
421

522
module.exports = getOrdinalNumber;
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
function repeatStr() {
2+
23
// 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).
34
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
5+
if (count < 0) {
6+
throw new Error("Count must be a non-negative integer");
7+
}
8+
return str.repeat(count);
59
}
610

711
module.exports = repeatStr;

0 commit comments

Comments
 (0)