├── src ├── fibonacci.js ├── factorial.js ├── primalidad.js └── _test_ │ ├── factorial.test.js │ ├── fibonacci.test.js │ └── primalidad.test.js ├── PULL_REQUEST_TEMPLATE.md ├── .github └── workflows │ └── mail.yml ├── package.json ├── LICENSE ├── README.md └── .gitignore /src/fibonacci.js: -------------------------------------------------------------------------------- 1 | const fibonacci = (n) => { 2 | // your code here 3 | } 4 | 5 | module.exports = fibonacci; -------------------------------------------------------------------------------- /src/factorial.js: -------------------------------------------------------------------------------- 1 | const factorial = (number) => { 2 | // your code here 3 | } 4 | 5 | module.exports = factorial; -------------------------------------------------------------------------------- /src/primalidad.js: -------------------------------------------------------------------------------- 1 | const trialDivision = (number) => { 2 | // your code here 3 | } 4 | 5 | module.exports = trialDivision; -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## DESCRIPTION 2 | 3 | Nombre: 4 | Usuario Platzi: 5 | 6 | ## Reto: 7 | 8 | - [ ] Primer problema 9 | - [ ] Segundo problema 10 | - [ ] Tercer problema 11 | -------------------------------------------------------------------------------- /.github/workflows/mail.yml: -------------------------------------------------------------------------------- 1 | name: jest 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2.4.2 10 | - name: Install modules 11 | run: yarn 12 | - name: Run tests 13 | run: yarn test 14 | -------------------------------------------------------------------------------- /src/_test_/factorial.test.js: -------------------------------------------------------------------------------- 1 | const factorial = require('../factorial.js'); 2 | 3 | describe('factorial', () => { 4 | it('Debe calcular el numer factorial', () => { 5 | expect(factorial(0)).toBe(1); 6 | expect(factorial(1)).toBe(1); 7 | expect(factorial(5)).toBe(120); 8 | expect(factorial(8)).toBe(40320); 9 | expect(factorial(10)).toBe(3628800); 10 | }); 11 | }); -------------------------------------------------------------------------------- /src/_test_/fibonacci.test.js: -------------------------------------------------------------------------------- 1 | const fibonacci = require('../fibonacci.js'); 2 | 3 | describe('fibonacci', () => { 4 | it('Debe calcular fibonacci correctamente', () => { 5 | expect(fibonacci(1)).toEqual([1]); 6 | expect(fibonacci(2)).toEqual([1, 1]); 7 | expect(fibonacci(3)).toEqual([1, 1, 2]); 8 | expect(fibonacci(4)).toEqual([1, 1, 2, 3]); 9 | expect(fibonacci(5)).toEqual([1, 1, 2, 3, 5]); 10 | expect(fibonacci(6)).toEqual([1, 1, 2, 3, 5, 8]); 11 | expect(fibonacci(7)).toEqual([1, 1, 2, 3, 5, 8, 13]); 12 | expect(fibonacci(8)).toEqual([1, 1, 2, 3, 5, 8, 13, 21]); 13 | expect(fibonacci(9)).toEqual([1, 1, 2, 3, 5, 8, 13, 21, 34]); 14 | expect(fibonacci(10)).toEqual([1, 1, 2, 3, 5, 8, 13, 21, 34, 55]); 15 | }); 16 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "javascript-challenges", 3 | "version": "1.0.0", 4 | "description": "javascript-challenges", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/gndx/javascript-challenges.git" 12 | }, 13 | "keywords": [ 14 | "JavaScript" 15 | ], 16 | "author": "Oscar Barajas ", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/gndx/javascript-challenges/issues" 20 | }, 21 | "homepage": "https://github.com/gndx/javascript-challenges#readme", 22 | "devDependencies": { 23 | "jest": "29.0.1" 24 | }, 25 | "jest": { 26 | "verbose": true, 27 | "transformIgnorePatterns": [ 28 | "/(node_modules)/" 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Platzi Master 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/_test_/primalidad.test.js: -------------------------------------------------------------------------------- 1 | const trialDivision = require('../primalidad.js'); 2 | 3 | function primalityTest(testFunction) { 4 | expect(testFunction(1)).toBe(false); 5 | expect(testFunction(2)).toBe(true); 6 | expect(testFunction(3)).toBe(true); 7 | expect(testFunction(5)).toBe(true); 8 | expect(testFunction(11)).toBe(true); 9 | expect(testFunction(191)).toBe(true); 10 | expect(testFunction(191)).toBe(true); 11 | expect(testFunction(199)).toBe(true); 12 | 13 | expect(testFunction(-1)).toBe(false); 14 | expect(testFunction(0)).toBe(false); 15 | expect(testFunction(4)).toBe(false); 16 | expect(testFunction(6)).toBe(false); 17 | expect(testFunction(12)).toBe(false); 18 | expect(testFunction(14)).toBe(false); 19 | expect(testFunction(25)).toBe(false); 20 | expect(testFunction(192)).toBe(false); 21 | expect(testFunction(200)).toBe(false); 22 | expect(testFunction(400)).toBe(false); 23 | 24 | // It should also deal with floats. 25 | expect(testFunction(0.5)).toBe(false); 26 | expect(testFunction(1.3)).toBe(false); 27 | expect(testFunction(10.5)).toBe(false); 28 | } 29 | 30 | describe('trialDivision', () => { 31 | it('Debe detectar números primos', () => { 32 | primalityTest(trialDivision); 33 | }); 34 | }); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaScript - Challenges 2 | 3 | ### 1 Factorial 4 | 5 | En matemáticas, el factorial de un número entero no negativo 'n', es el producto de la multiplicación de todos los números enteros positivos menores o iguales que 'n'. Por ejemplo: 6 | 7 | 5 = 5 _ 4 _ 3 _ 2 _ 1 = 120 8 | 9 | ### 2 Prueba de primalidad 10 | 11 | Un número primo (o primo) es un número natural mayor que 1 que no se puede formar multiplicando dos números naturales más pequeños. Un número natural mayor que 1 que no es primo se llama número compuesto. Por ejemplo, 5 es primo porque las únicas formas de escribirlo como producto, 1 × 5 o 5 × 1, involucran a 5 en sí. Sin embargo, 6 es compuesto porque es el producto de dos números (2 × 3) que son más pequeños que 6. 12 | 13 | ### 3 Numero de Fibonacci 14 | 15 | En matemáticas, los números de Fibonacci son los números en la siguiente secuencia entera, llamada secuencia de Fibonacci, y se caracterizan por el hecho de que cada número después de los dos primeros es la suma de los dos anteriores: 16 | 17 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 18 | 19 | ### Instalación 20 | 21 | ``` 22 | npm install 23 | ``` 24 | 25 | ### test 26 | 27 | ``` 28 | npm run test 29 | ``` 30 | 31 | ### RETO 32 | 33 | 1. Genera una función que devuelve numero factorial que se le pase como parametro. Ejemplo: 34 | 35 | ``` 36 | 5 = 120, 37 | 9 = 362 880, 38 | 15 = 1 307 674 368 000. 39 | ``` 40 | 41 | 1. Genera un algoritmo para determinar si un número de entrada es primo o no. 42 | 2. Genera una función que devuelve una secuencia de Fibonacci como una array 43 | 44 | ### Enviar solución de reto 45 | 46 | Debes hacer un "Fork" de este proyecto, revolver los problemas y crear un Pull Request hacia este repositorio. 47 | 48 | ### Contribuir 49 | 50 | Si alguien quiere agregar o mejorar algo, lo invito a colaborar directamente en este repositorio: [javascript-challenges](https://github.com/platzimaster/gndx/javascript-challenges) 51 | 52 | ### Licencia 53 | 54 | javascript-challenges se lanza bajo la licencia [MIT](https://opensource.org/licenses/MIT). 55 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | --------------------------------------------------------------------------------