Skip to content

Commit 7fabb60

Browse files
committed
applied changes to ordinal and repeat functions and tests
1 parent d933572 commit 7fabb60

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ function getOrdinalNumber(num) {
22
if (arguments.length !== 1) {
33
throw new Error("Function requires exactly one argument");
44
}
5-
if (typeof num !== "number" || isNaN(num)) {
5+
if (typeof num !== "number") {
66
throw new Error("Input must be a number");
77
}
8-
if (!Number.isFinite(num)){
8+
if (!isFinite(num)) {
99
throw new Error("Input must be a finite number");
1010
}
1111
if (!Number.isInteger(num) || num < 0) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test("should throw an error if the argument is a decimal", () => {
8383
});
8484

8585
test("should throw an error if the argument is NaN", () => {
86-
expect(() => getOrdinalNumber(NaN)).toThrow("Input must be a number");
86+
expect(() => getOrdinalNumber(NaN)).toThrow("Input must be a finite number");
8787
});
8888

8989
test("should throw an error if the argument is Infinity", () => {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
function repeat(str, count) {
22
if ( arguments.length !== 2) {
3-
return "Function requires exactly 2 arguments";
3+
throw new Error(`Function requires exactly two arguments: a string and a count. Received ${arguments.length} arguments`);
4+
45
}
56
if (typeof str !== "string") {
6-
return "First argument must be a string";
7+
throw new Error("First argument must be a string. Received type " + typeof str);
78
}
89
if (!Number.isInteger(count) || count < 0) {
9-
return "Second argument must be a non-negative integer";
10+
throw new Error("Second argument must be a non-negative integer. Received " + count );
1011
}
1112
return str.repeat(count);
1213
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,56 +46,56 @@ test("should return empty string if count is 0", () => {
4646
test("should return error message for negative count", () => {
4747
const str = "hello";
4848
const count = -2;
49-
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
49+
expect(() => repeat(str, count)).toThrow("Second argument must be a non-negative integer");
5050
});
5151

5252
// invalid input tests
5353
test("should return error message for non-integer count", () => {
5454
const str = "hello";
5555
const count = 2.5;
56-
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
56+
expect(() => repeat(str, count)).toThrow("Second argument must be a non-negative integer");
5757
});
5858

5959
test("should return error message for non-string input", () => {
6060
const str = 123;
6161
const count = 3;
62-
expect(repeat(str, count)).toEqual("First argument must be a string");
62+
expect(() => repeat(str, count)).toThrow("First argument must be a string");
6363
});
6464

6565
test("should return error message for non-string input with invalid count", () => {
6666
const str = { text: "hello" };
6767
const count = -2;
68-
expect(repeat(str, count)).toEqual("First argument must be a string");
68+
expect(() => repeat(str, count)).toThrow("First argument must be a string");
6969
});
7070

7171
test("should return error message for string input with non number count", () => {
7272
const str = "hello";
7373
const count = "3";
7474
const count2 = [];
75-
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
76-
expect(repeat(str, count2)).toEqual("Second argument must be a non-negative integer");
75+
expect(() => repeat(str, count)).toThrow("Second argument must be a non-negative integer");
76+
expect(() => repeat(str, count2)).toThrow("Second argument must be a non-negative integer");
7777
});
7878

7979
test("should return error message for string input with NaN count", () => {
8080
const str = "hello";
8181
const count = NaN;
82-
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
82+
expect(() => repeat(str, count)).toThrow("Second argument must be a non-negative integer");
8383
});
8484

8585
test("should return error message for string input with null count", () => {
8686
const str = "hello";
8787
const count = null;
88-
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
88+
expect(() => repeat(str, count)).toThrow("Second argument must be a non-negative integer");
8989
});
9090

9191
test("should return error message for string input with undefined count", () => {
9292
const str = "hello";
9393
const count = undefined;
94-
expect(repeat(str, count)).toEqual("Second argument must be a non-negative integer");
94+
expect(() => repeat(str, count)).toThrow("Second argument must be a non-negative integer. Received undefined");
9595
});
9696

9797
test('should have the correct amount of arguments', () => {
98-
expect(repeat('hello')).toEqual("Function requires exactly 2 arguments");
99-
expect(repeat("hello", 3, 3)).toEqual("Function requires exactly 2 arguments");
98+
expect(() => repeat('hello')).toThrow(new Error("Function requires exactly two arguments: a string and a count. Received 1 arguments"));
99+
expect(() => repeat("hello", 3, 3)).toThrow(new Error("Function requires exactly two arguments: a string and a count. Received 3 arguments"));
100100
})
101101

0 commit comments

Comments
 (0)