Skip to content

Commit 882d418

Browse files
committed
feat/ update function to not accept strings
1 parent dc40770 commit 882d418

File tree

4 files changed

+7
-32
lines changed

4 files changed

+7
-32
lines changed

13_factorial/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ Write a recursive function that takes a non-negative integer, and returns the pr
55
For example:
66

77
```javascript
8-
factorial(2); // 2 * 1, Output: 2
9-
factorial('4'); // 4 * 3 * 2 * 1, Output: 24
8+
factorial('4'); // Output: undefined
109
factorial(5); // 5 * 4 * 3 * 2 * 1, Output: 120
1110
factorial(7.2); // Output: undefined
1211
factorial(0); // Output: 1

13_factorial/factorial.spec.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,7 @@ describe('factorial', () => {
2525
test.skip('doesn\'t accept floats', () => {
2626
expect(factorial(5.4)).toBe(undefined);
2727
});
28-
test.skip('DOES accept strings', () => {
29-
expect(factorial("0")).toBe(1);
30-
});
31-
test.skip('DOES accept strings', () => {
32-
expect(factorial("1")).toBe(1);
33-
});
34-
test.skip('DOES accept strings', () => {
35-
expect(factorial("2")).toBe(2);
36-
});
37-
test.skip('DOES accept strings', () => {
38-
expect(factorial("8")).toBe(40320);
28+
test.skip('doesn\'t accept strings', () => {
29+
expect(factorial('5')).toBe(undefined);
3930
});
4031
});

13_factorial/solution/factorial-solution.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
const factorial = function(n) {
2-
// a negative number will always contain a '-'
3-
// a non-integer will always contain a '.'
4-
// the [-.] regex literal will match strings that contain a '-' or a '.'
5-
// n + '' converts n to a string, which is required for the Regex.prototype.test method
6-
if (/[-.]/.test(n + '')) return;
7-
8-
// +n converts n to a number
9-
if (+n === 0) return 1;
2+
if (!Number.isInteger(n) || n < 0) return;
3+
if (n === 0) return 1;
104
return n * factorial(n - 1);
115
};
126

13_factorial/solution/factorial-solution.spec.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,7 @@ describe('factorial', () => {
2525
test.skip('doesn\'t accept floats', () => {
2626
expect(factorial(5.4)).toBe(undefined);
2727
});
28-
test.skip('DOES accept strings', () => {
29-
expect(factorial("0")).toBe(1);
30-
});
31-
test.skip('DOES accept strings', () => {
32-
expect(factorial("1")).toBe(1);
33-
});
34-
test.skip('DOES accept strings', () => {
35-
expect(factorial("2")).toBe(2);
36-
});
37-
test.skip('DOES accept strings', () => {
38-
expect(factorial("8")).toBe(40320);
28+
test.skip('doesn\'t accept strings', () => {
29+
expect(factorial('5')).toBe(undefined);
3930
});
4031
});

0 commit comments

Comments
 (0)