mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-06-05 15:09:09 +09:00
added explicit conversion to number for fibonacci solution
This commit is contained in:
@@ -1,4 +1,13 @@
|
|||||||
const fibonacci = function(count) {
|
const fibonacci = function(countArg) {
|
||||||
|
// checks argument's type and makes sure we use
|
||||||
|
// a number throughout rest of function.
|
||||||
|
let count
|
||||||
|
if (typeof countArg !== 'number') {
|
||||||
|
count = parseInt(countArg)
|
||||||
|
} else {
|
||||||
|
count = countArg
|
||||||
|
}
|
||||||
|
|
||||||
if (count < 0) return "OOPS";
|
if (count < 0) return "OOPS";
|
||||||
if (count == 0) return 0;
|
if (count == 0) return 0;
|
||||||
|
|
||||||
@@ -14,4 +23,4 @@ const fibonacci = function(count) {
|
|||||||
return firstPrev;
|
return firstPrev;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = fibonacci;
|
module.exports = fibonacci;
|
||||||
@@ -34,4 +34,4 @@ describe('fibonacci', () => {
|
|||||||
test('DOES accept strings', () => {
|
test('DOES accept strings', () => {
|
||||||
expect(fibonacci("8")).toBe(21);
|
expect(fibonacci("8")).toBe(21);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user