From 49abf27fd4ce8a6a91fb9931e7b72d0ab618596e Mon Sep 17 00:00:00 2001 From: Kornel Filep Date: Sat, 8 Aug 2020 14:16:00 +0200 Subject: [PATCH] module-1 solutions added --- module-1/classification.js | 6 +----- module-1/euclidean.js | 16 +++++++++++----- module-1/fibonacci.js | 9 +++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/module-1/classification.js b/module-1/classification.js index 9f85b22..b1b11c6 100644 --- a/module-1/classification.js +++ b/module-1/classification.js @@ -11,7 +11,6 @@ * @returns {number} grade or 0 if any arguments are not proper */ function grade(score) { - let gradeOfStudent; /* * Your task is to calculate the grade of the student * based on his/her score which can be found in the @@ -20,9 +19,6 @@ function grade(score) { * Store the grade in the gradeOfStudent variable. * Also take into consideration the documentation of the function! */ - // PLACE YOUR CODE BETWEEN THIS... - - // ...AND THIS COMMENT LINE! - return gradeOfStudent; + return (score > 100 ? 0 : score >= 90 ? 5 : score >= 80 ? 4 : score >= 70 ? 3 : score >= 60 ? 2 : score < 0 ? 0 : 1); } module.exports = grade; \ No newline at end of file diff --git a/module-1/euclidean.js b/module-1/euclidean.js index 3d33d00..bd91332 100644 --- a/module-1/euclidean.js +++ b/module-1/euclidean.js @@ -7,7 +7,6 @@ * @returns {number} GCD or 0 if any arguments are not proper */ function euclidean(a, b) { - let gcd; /* * Your task is to compute the greatest common divisor of * the numbers given in a and b variables, using the @@ -15,10 +14,17 @@ function euclidean(a, b) { * If you have the result, assign it to the variable, called gcd. * Also take into consideration the documentation of the function! */ - // PLACE YOUR CODE BETWEEN THIS... + if (a <= 0 || b <= 0) { + return 0; + } - - // ...AND THIS COMMENT LINE! - return gcd; + while (a !== b) { + if (a > b) { + a -= b; + } else { + b -= a; + } + } + return a; } module.exports = euclidean; \ No newline at end of file diff --git a/module-1/fibonacci.js b/module-1/fibonacci.js index 14ec907..92e8658 100644 --- a/module-1/fibonacci.js +++ b/module-1/fibonacci.js @@ -16,6 +16,15 @@ function fibonacci(n) { */ // PLACE YOUR CODE BETWEEN THIS... + if (n < 0) { + return 0 + } + + if (n < 2) { + nThFibonacci = n + } else { + nThFibonacci = fibonacci(n-2)+fibonacci(n-1) + } // ...AND THIS COMMENT LINE! return nThFibonacci; }