Skip to content

Commit d30100c

Browse files
Resolve review comments for repeatStr and getOrdinalNumber exercises
1 parent f961fe6 commit d30100c

5 files changed

Lines changed: 41 additions & 19 deletions

File tree

14.1 KB
Loading
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
function getOrdinalNumber(num) {
2-
if (num % 10 === 1 && num % 100 !== 11) {
3-
return num + "st";
2+
if (num % 100 >= 11 && num % 100 <= 13) {
3+
return num + "th";
44
}
5+
6+
if (num % 10 === 1) {
7+
return num + "st";
8+
}
9+
10+
if (num % 10 === 2) {
11+
return num + "nd";
12+
}
13+
14+
if (num % 10 === 3) {
15+
return num + "rd";
16+
}
17+
18+
return num + "th";
519
}
620

721
module.exports = getOrdinalNumber;

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ const getOrdinalNumber = require("./get-ordinal-number");
1414
// When the number ends with 1, except those ending with 11,
1515
// Then the function should return a string by appending "st" to the number.
1616
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
17-
expect(getOrdinalNumber(1)).toEqual("1st");
17+
expect(getOrdinalNumber(1)).toEqual("1st");
18+
expect(getOrdinalNumber(2)).toEqual("2nd");
19+
expect(getOrdinalNumber(3)).toEqual("3rd");
20+
expect(getOrdinalNumber(4)).toEqual("4th");
21+
22+
expect(getOrdinalNumber(11)).toEqual("11th");
23+
expect(getOrdinalNumber(12)).toEqual("12th");
24+
expect(getOrdinalNumber(13)).toEqual("13th");
25+
1826
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
expect(getOrdinalNumber(23)).toEqual("23rd");
29+
expect(getOrdinalNumber(24)).toEqual("24th");
2030
});
31+
32+
// ![image-comment](.image-comment/image-20260704-131119-kvvnah.png)
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
function repeatStr() {
2-
// 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).
3-
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
5-
}
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count must be a non-negative integer");
4+
} else if (count === 0) {
5+
return "";
6+
} else {
7+
return str.repeat(count);
8+
}
9+
}
610

711
module.exports = repeatStr;

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Implement a function repeatStr
2-
2+
const repeatStr = require("./repeat-str");
33
// Given a target string `str` and a positive integer `count`,
44
// When the repeatStr function is called with these inputs,
55
// Then it should:
@@ -9,15 +9,7 @@
99
// When the repeatStr function is called with these inputs,
1010
// Then it should return a string that contains the original `str` repeated `count` times.
1111

12-
function repeatStr(str, count) {
13-
if (count < 0) {
14-
throw new Error("Count must be a non-negative integer");
15-
} else if (count === 0) {
16-
return "";
17-
} else {
18-
return str.repeat(count);
19-
}
20-
}
12+
2113

2214
test("should repeat the string count times", () => {
2315
const str = "hello";

0 commit comments

Comments
 (0)