From 9f122cd1a952f579e9506854b0d23b833ef481e3 Mon Sep 17 00:00:00 2001 From: Karina Algarra Date: Mon, 8 Jun 2020 22:45:55 -0500 Subject: [PATCH 1/2] First Problem --- src/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index d6fa599..b626635 100644 --- a/src/index.js +++ b/src/index.js @@ -1,18 +1,18 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; - var API = 'https://rickandmortyapi.com/api/character/'; var xhttp = new XMLHttpRequest(); function fetchData(url_api, callback) { + var xhttp = new XMLHttpRequest(); + xhttp.open('GET', url_api, false); + xhttp.send(); xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === '4') { + if (xhttp.readyState === 4) { if (xhttp.status == 200) - callback(null, xhttp.responseText); + callback(null,JSON.parse (xhttp.responseText)); else return callback(url_api); } }; - xhttp.open('GET', url_api, false); - xhttp.send(); + }; fetchData(API, function (error1, data1) { From 031e6c92fc2ce44f675fcd817484c34af47776fd Mon Sep 17 00:00:00 2001 From: Karina Algarra Date: Mon, 21 Sep 2020 21:11:51 -0500 Subject: [PATCH 2/2] [add]changes --- .gitignore | 122 +++++++++++++++++++-------------------- LICENSE | 42 +++++++------- PULL_REQUEST_TEMPLATE.md | 26 ++++----- README.md | 62 ++++++++++---------- package-lock.json | 13 +++++ package.json | 54 ++++++++--------- src/index.js | 69 ++++++++++++---------- 7 files changed, 203 insertions(+), 185 deletions(-) create mode 100644 package-lock.json diff --git a/.gitignore b/.gitignore index ad46b30..bbd57d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,61 +1,61 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# TypeScript v1 declaration files -typings/ - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variables file -.env - -# next.js build output -.next +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# next.js build output +.next diff --git a/LICENSE b/LICENSE index 9905d73..0152f72 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -MIT License - -Copyright (c) 2019 Comunidad Platzi - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +MIT License + +Copyright (c) 2019 Comunidad Platzi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..995538a 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ -## DESCRIPTION - -Nombre: -Usuario Platzi: - -## Ciudad -- [ ] Ciudad de México -- [ ] Bogotá - -# Retos: - - [ ] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema +## DESCRIPTION + +Nombre: +Usuario Platzi: + +## Ciudad +- [ ] Ciudad de México +- [ ] Bogotá + +# Retos: + - [ ] Primer problema + - [ ] Segundo problema + - [ ] Tercer problema diff --git a/README.md b/README.md index ae49276..294f37d 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,31 @@ -# javascript-challenge -Reto Fundamentos de JavaScript - -# Instalación - -``` -npm install -``` -# Ejecución - -``` -npm run start -``` - -### Primer problema -La aplicación tiene errores que no permiten ejecutarla, lee detenidamente el código y determina dónde se encuentran al ejecutarlo en la consola. - -### Segundo Problema -Una vez que tu aplicación ya esté funcionando convierte el código a ECMAScript 6 (ES6) -* Arrow Functions -* Template Strings - -### Tercer Problema -Transforma el código escrito en ECMAScript6(ES6) para que funcione con promesas y así evitar el Callback Hell del final. - -# Contributing -If someone wants to add or improve something, I invite you to collaborate directly in this repository: [javascript-challenge](https://github.com/gndx/javascript-challenge/) - -# License - -javascript-challenge is released under the [MIT License](https://opensource.org/licenses/MIT). +# javascript-challenge +Reto Fundamentos de JavaScript + +# Instalación + +``` +npm install +``` +# Ejecución + +``` +npm run start +``` + +### Primer problema +La aplicación tiene errores que no permiten ejecutarla, lee detenidamente el código y determina dónde se encuentran al ejecutarlo en la consola. + +### Segundo Problema +Una vez que tu aplicación ya esté funcionando convierte el código a ECMAScript 6 (ES6) +* Arrow Functions +* Template Strings + +### Tercer Problema +Transforma el código escrito en ECMAScript6(ES6) para que funcione con promesas y así evitar el Callback Hell del final. + +# Contributing +If someone wants to add or improve something, I invite you to collaborate directly in this repository: [javascript-challenge](https://github.com/gndx/javascript-challenge/) + +# License + +javascript-challenge is released under the [MIT License](https://opensource.org/licenses/MIT). diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b7c321c --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "escuelajs-reto-03", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "xmlhttprequest": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz", + "integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw=" + } + } +} diff --git a/package.json b/package.json index 65a8bf2..6497c14 100644 --- a/package.json +++ b/package.json @@ -1,27 +1,27 @@ -{ - "name": "escuelajs-reto-03", - "version": "1.0.0", - "description": "Reto 3 Septiembre 14: Curso de Fundamentos de JavaScript", - "main": "index.js", - "scripts": { - "start": "node src/index.js" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/platzi/escuelajs-reto-03.git" - }, - "keywords": [ - "javascript", - "escuelajs", - "node" - ], - "author": "Oscar Barajas ", - "license": "MIT", - "bugs": { - "url": "https://github.com/platzi/escuelajs-reto-03/issues" - }, - "homepage": "https://github.com/platzi/escuelajs-reto-03#readme", - "dependencies": { - "xmlhttprequest": "^1.8.0" - } -} \ No newline at end of file +{ + "name": "escuelajs-reto-03", + "version": "1.0.0", + "description": "Reto 3 Septiembre 14: Curso de Fundamentos de JavaScript", + "main": "index.js", + "scripts": { + "start": "node src/index.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/platzi/escuelajs-reto-03.git" + }, + "keywords": [ + "javascript", + "escuelajs", + "node" + ], + "author": "Oscar Barajas ", + "license": "MIT", + "bugs": { + "url": "https://github.com/platzi/escuelajs-reto-03/issues" + }, + "homepage": "https://github.com/platzi/escuelajs-reto-03#readme", + "dependencies": { + "xmlhttprequest": "^1.8.0" + } +} diff --git a/src/index.js b/src/index.js index b626635..358534a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,37 @@ -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); - -function fetchData(url_api, callback) { - var xhttp = new XMLHttpRequest(); - xhttp.open('GET', url_api, false); - xhttp.send(); - xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === 4) { - if (xhttp.status == 200) - callback(null,JSON.parse (xhttp.responseText)); - else return callback(url_api); - } - }; - -}; - -fetchData(API, function (error1, data1) { - if (error1) return console.error('Error' + ' ' + error1); - console.log('Primer Llamado...') - fetchData(API + data1.results[0].id, function (error2, data2) { - if (error2) return console.error(error1); - console.log('Segundo Llamado...') - fetchData(data2.origin.url, function (error3, data3) { - if (error3) return console.error(error3); - console.log('Tercero Llamado...') - console.log('Personajes:' + ' ' + data1.info.count); - console.log('Primer Personaje:' + ' ' + data2.name); - console.log('Dimensión:' + ' ' + data3.dimension); - }); - }); -}); \ No newline at end of file +const API = 'https://rickandmortyapi.com/api/character/'; +const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest + +const fetchData = (url_api) => { + const xhttp = new XMLHttpRequest(); + return new Promise((resolve, reject) => { + xhttp.open("GET", url_api, true); + xhttp.onreadystatechange = (event) => { + if (xhttp.readyState === 4) { + if (xhttp.status == 200) { + resolve(JSON.parse(xhttp.responseText)); + } else { + reject(new Error("Wooops! " + url_api)); + } + } + }; + xhttp.send(); + }); +}; + +const fetchD1 = async (url_api) => { + try { + const response = await fetchData(url_api); + const data = await fetchData(`${API}${response.results[0].id}`); + const origin = await fetchData(data.origin.url); + console.log("Primer Llamado..."); + console.log(`Personajes: ${response.info.count}`); + console.log("Segundo Llamado..."); + console.log(`Primer Personaje: ${data.name}`); + console.log("Tercero Llamado..."); + console.log(`Dimensión: ${origin.dimension}`); + } catch (error) { + console.error(error); + } +}; + +fetchD1(API);