From 5fc115e4ffc9304be5718174dcc5046de4be8f85 Mon Sep 17 00:00:00 2001 From: LoreNa Date: Sun, 27 Sep 2020 16:18:12 -0500 Subject: [PATCH 1/4] Parte 1 reto JS --- .vscode/launch.json | 15 +++++++++++++++ PULL_REQUEST_TEMPLATE.md | 8 ++++---- src/index.js | 8 ++++---- 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..7a9dfa0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-chrome", + "request": "launch", + "name": "Launch Chrome against localhost", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..3cce517 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION -Nombre: -Usuario Platzi: +Nombre: Lorena Mosquera González +Usuario Platzi: @Lorebratt ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [X] Bogotá # Retos: - - [ ] Primer problema + - [X] Primer problema - [ ] Segundo problema - [ ] Tercer problema diff --git a/src/index.js b/src/index.js index d6fa599..8c01078 100644 --- a/src/index.js +++ b/src/index.js @@ -3,17 +3,17 @@ var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var API = 'https://rickandmortyapi.com/api/character/'; var xhttp = new XMLHttpRequest(); -function fetchData(url_api, callback) { +function fetchData(url_api, callback) 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) { if (error1) return console.error('Error' + ' ' + error1); From da36e0b78a1b207fed08970060e4dba7a561faae Mon Sep 17 00:00:00 2001 From: LoreNa Date: Sun, 27 Sep 2020 16:54:02 -0500 Subject: [PATCH 2/4] Parte 2 reto JS --- PULL_REQUEST_TEMPLATE.md | 6 +++--- src/index.js | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 3cce517..a75dd9e 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -8,6 +8,6 @@ Usuario Platzi: @Lorebratt - [X] Bogotá # Retos: - - [X] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema + - [] Primer problema + - [X] Segundo problema + - [] Tercer problema diff --git a/src/index.js b/src/index.js index 8c01078..69a8a50 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,8 @@ var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var API = 'https://rickandmortyapi.com/api/character/'; var xhttp = new XMLHttpRequest(); -function fetchData(url_api, callback) - xhttp.onreadystatechange = function (event) { +const fetchData =(url_api, callback) => { + xhttp.onreadystatechange = (event) => { if (xhttp.readyState === 4) { if (xhttp.status == 200) callback(null, JSON.parse(xhttp.responseText)); @@ -13,20 +13,20 @@ function fetchData(url_api, callback) }; xhttp.open('GET', url_api, false); xhttp.send(); +} - -fetchData(API, function (error1, data1) { +fetchData('${API}', (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); + fetchData('${API} ${data1.results[0].id}', (error2, data2) => { + if (error2) return console.error(error2); console.log('Segundo Llamado...') - fetchData(data2.origin.url, function (error3, data3) { + fetchData(data2.origin.url, (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); + console.log('Personajes: ${data1.info.count}'); + console.log('Primer Personaje: ${data2.name}'); + console.log('Dimensión: ${data3.dimension}'); }); }); }); \ No newline at end of file From 4338f7f78a60118d0c0f7d26281d93bcb2d9114d Mon Sep 17 00:00:00 2001 From: LoreNa Date: Sun, 27 Sep 2020 17:25:19 -0500 Subject: [PATCH 3/4] Parte 2 reto JS ok --- package-lock.json | 13 +++++++++++++ package.json | 2 +- src/index.js | 12 ++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e8c8dbc --- /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..dbaa2f1 100644 --- a/package.json +++ b/package.json @@ -24,4 +24,4 @@ "dependencies": { "xmlhttprequest": "^1.8.0" } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 69a8a50..6106265 100644 --- a/src/index.js +++ b/src/index.js @@ -15,18 +15,18 @@ const fetchData =(url_api, callback) => { xhttp.send(); } -fetchData('${API}', (error1, data1) => { +fetchData(`${API}`, (error1, data1) => { if (error1) return console.error('Error' + ' ' + error1); console.log('Primer Llamado...') - fetchData('${API} ${data1.results[0].id}', (error2, data2) => { + fetchData(`${API}${data1.results[0].id}`, (error2, data2) => { if (error2) return console.error(error2); console.log('Segundo Llamado...') fetchData(data2.origin.url, (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}'); + 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 From 8cdcb75766243d579214a2d9a4b432a62a908625 Mon Sep 17 00:00:00 2001 From: LoreNa Date: Sun, 27 Sep 2020 18:24:14 -0500 Subject: [PATCH 4/4] Parte 3 reto JS --- PULL_REQUEST_TEMPLATE.md | 4 +-- src/index.js | 55 ++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index a75dd9e..0f5cf21 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -9,5 +9,5 @@ Usuario Platzi: @Lorebratt # Retos: - [] Primer problema - - [X] Segundo problema - - [] Tercer problema + - [] Segundo problema + - [X] Tercer problema diff --git a/src/index.js b/src/index.js index 6106265..c4015bf 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,37 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); +const API = 'https://rickandmortyapi.com/api/character/'; +const xhttp = new XMLHttpRequest(); -const fetchData =(url_api, callback) => { + +const fetchData = (url_api) => { + xhttp.responseType = 'json'; + xhttp.open('GET', url_api, true); + xhttp.send(); + return new Promise((resolve, reject) => { xhttp.onreadystatechange = (event) => { if (xhttp.readyState === 4) { - if (xhttp.status == 200) - callback(null, JSON.parse(xhttp.responseText)); - else return callback(url_api); + if (xhttp.status == 200){ + const response = JSON.parse(xhttp.responseText); + resolve(response); + } + else reject(Error(`${url_api} request did not load succesfully`)); } - }; - xhttp.open('GET', url_api, false); - xhttp.send(); + } + }) } -fetchData(`${API}`, (error1, data1) => { - if (error1) return console.error('Error' + ' ' + error1); - console.log('Primer Llamado...') - fetchData(`${API}${data1.results[0].id}`, (error2, data2) => { - if (error2) return console.error(error2); - console.log('Segundo Llamado...') - fetchData(data2.origin.url, (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 +fetchData(`${API}`).then(data1 => { + console.log(`Primer Llamado...`); + console.log(`Personajes: ${data1.info.count}`); + return fetchData(`${API}${data1.results[0].id}`) +}).then(data2 => { + console.log(`Segundo Llamado...`); + console.log(`Primer Personaje: ${data2.name}`); + return fetchData(`${data2.origin.url}`) +}).then(data3 => { + console.log(`Tercer Llamado...`); + console.log(`Dimensión: ${data3.dimension}`); +}).catch(error => { + console.log(error) +}) \ No newline at end of file