From e3f956ebb020864ea0570359525965936611d3a8 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 6 Sep 2022 13:38:33 -0500 Subject: [PATCH] =?UTF-8?q?A=C3=B1adiendo=20soluciones?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 6 +++--- src/factorial.js | 7 +++++++ src/fibonacci.js | 12 ++++++++++++ src/primalidad.js | 8 ++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index fd061e8..92817cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { - "name": "challenge-js-03", + "name": "javascript-challenges", "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "challenge-js-03", + "name": "javascript-challenges", "version": "1.0.0", "license": "MIT", "devDependencies": { - "jest": "^29.0.1" + "jest": "29.0.1" } }, "node_modules/@ampproject/remapping": { diff --git a/src/factorial.js b/src/factorial.js index 4f3ae70..31bc286 100644 --- a/src/factorial.js +++ b/src/factorial.js @@ -1,5 +1,12 @@ const factorial = (number) => { // your code here + if (number < 0) throw new Error("Entrada no vĂ¡lida"); + + result = 1; + + for (let i = number; i > 1; i--) result *= i; + + return result; } module.exports = factorial; \ No newline at end of file diff --git a/src/fibonacci.js b/src/fibonacci.js index ea3270f..e3ec4a3 100644 --- a/src/fibonacci.js +++ b/src/fibonacci.js @@ -1,5 +1,17 @@ const fibonacci = (n) => { // your code here + // 0 1 1 2 3 5 8 + let a = 0, b = 1; + const fibonacciSerie = []; + + for (let i = 1; i <= n; i++) { + fibonacciSerie.push(b); + const aux = a; + a = b; + b = aux + b; + } + + return fibonacciSerie; } module.exports = fibonacci; \ No newline at end of file diff --git a/src/primalidad.js b/src/primalidad.js index 8bdb849..ee64720 100644 --- a/src/primalidad.js +++ b/src/primalidad.js @@ -1,5 +1,13 @@ const trialDivision = (number) => { // your code here + if (number !== Math.round(number)) return false; + + const divisores = []; + for (let i = 2; i < 10; i++) + if (i !== number && number % i === 0) divisores.push(i); + + // console.log(divisores); + return number > 1 && divisores.length === 0; } module.exports = trialDivision; \ No newline at end of file