From 80ca665767a48a52f2a7c57510f384b89eec3b43 Mon Sep 17 00:00:00 2001 From: Jamienorthman Date: Sun, 29 Oct 2023 12:36:58 +0100 Subject: [PATCH] added explicit conversion to number for fibonacci solution --- 10_fibonacci/solution/fibonacci-solution.js | 13 +++++++++++-- 10_fibonacci/solution/fibonacci-solution.spec.js | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.js b/10_fibonacci/solution/fibonacci-solution.js index a002ffe..953a5e1 100644 --- a/10_fibonacci/solution/fibonacci-solution.js +++ b/10_fibonacci/solution/fibonacci-solution.js @@ -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 0; @@ -14,4 +23,4 @@ const fibonacci = function(count) { return firstPrev; }; -module.exports = fibonacci; +module.exports = fibonacci; \ No newline at end of file diff --git a/10_fibonacci/solution/fibonacci-solution.spec.js b/10_fibonacci/solution/fibonacci-solution.spec.js index 33023e8..fdb0bad 100644 --- a/10_fibonacci/solution/fibonacci-solution.spec.js +++ b/10_fibonacci/solution/fibonacci-solution.spec.js @@ -34,4 +34,4 @@ describe('fibonacci', () => { test('DOES accept strings', () => { expect(fibonacci("8")).toBe(21); }); -}); +}); \ No newline at end of file