From 1008f02e3acae387d8a20e331f73cb8273e31c91 Mon Sep 17 00:00:00 2001 From: Ilona Khain Date: Mon, 14 Jun 2021 17:26:30 +0300 Subject: [PATCH] add solution for module 1: js basics --- module-1/classification.js | 2 +- module-1/euclidean.js | 16 ++++++++++++++-- module-1/fibonacci.js | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/module-1/classification.js b/module-1/classification.js index 9f85b22..98a55f4 100644 --- a/module-1/classification.js +++ b/module-1/classification.js @@ -21,7 +21,7 @@ function grade(score) { * Also take into consideration the documentation of the function! */ // PLACE YOUR CODE BETWEEN THIS... - + gradeOfStudent = (score >= 0 && score <= 100) ? Math.max(Math.min(Math.floor(score / 10) - 4, 5), 1) : 0; // ...AND THIS COMMENT LINE! return gradeOfStudent; } diff --git a/module-1/euclidean.js b/module-1/euclidean.js index 3d33d00..59a3f34 100644 --- a/module-1/euclidean.js +++ b/module-1/euclidean.js @@ -16,8 +16,20 @@ function euclidean(a, b) { * Also take into consideration the documentation of the function! */ // PLACE YOUR CODE BETWEEN THIS... - - + if (a > 0 && b > 0 && typeof (a) === 'number' && typeof (b) === 'number') { + while (a != b) { + if (a > b) { + a -= b; + } + else { + b -= a; + } + } + gcd = a; + } + else { + gcd = 0; + } // ...AND THIS COMMENT LINE! return gcd; } diff --git a/module-1/fibonacci.js b/module-1/fibonacci.js index 14ec907..1262ab0 100644 --- a/module-1/fibonacci.js +++ b/module-1/fibonacci.js @@ -15,7 +15,23 @@ function fibonacci(n) { * Also take into consideration the documentation of the function! */ // PLACE YOUR CODE BETWEEN THIS... - + if (n >= 0) { + if (n < 2 && typeof (n) === 'number') { + nThFibonacci = n; + } else { + let f0 = 0; + let f1 = 1; + for (let i = 2; i <= n; ++i) { + const sum = f0 + f1; + f0 = f1; + f1 = sum; + } + nThFibonacci = f1; + } + } + else { + nThFibonacci = 0; + } // ...AND THIS COMMENT LINE! return nThFibonacci; }