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..67663ec 100644 --- a/src/factorial.js +++ b/src/factorial.js @@ -1,5 +1,7 @@ const factorial = (number) => { - // your code here -} + if (number === 0) return 1; + else if (number < 0) return -1; + else return number * factorial(number - 1); +}; -module.exports = factorial; \ No newline at end of file +module.exports = factorial; diff --git a/src/fibonacci.js b/src/fibonacci.js index ea3270f..341eec3 100644 --- a/src/fibonacci.js +++ b/src/fibonacci.js @@ -1,5 +1,12 @@ const fibonacci = (n) => { - // your code here -} + let array = [1, 1]; + if (n === 1) return [1]; + if (n === 2) return array; -module.exports = fibonacci; \ No newline at end of file + for (let i = 0; i < n - 2; i++) { + array[i + 2] = array[i] + array[i + 1]; + } + return array; +}; + +module.exports = fibonacci; diff --git a/src/primalidad.js b/src/primalidad.js index 8bdb849..af6d188 100644 --- a/src/primalidad.js +++ b/src/primalidad.js @@ -1,5 +1,12 @@ const trialDivision = (number) => { - // your code here -} + let bandera = true; + if (number <= 1) return false; + if (!Number.isInteger(number)) return false; + for (let i = 2; i <= Math.sqrt(number); i++) { + if (number % i === 0) bandera = false; + } + return bandera; +}; +trialDivision(5); -module.exports = trialDivision; \ No newline at end of file +module.exports = trialDivision;