-
Notifications
You must be signed in to change notification settings - Fork 10
Ahmad_Zetouny-w2-UsignAPIs #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
## Test Summary | ||
|
||
**Mentors**: For more information on how to review homework assignments, please refer to the [Review Guide](https://github.com/HackYourFuture/mentors/blob/main/assignment-support/review-guide.md). | ||
|
||
### 3-UsingAPIs - Week2 | ||
|
||
| Exercise | Passed | Failed | ESLint | | ||
|-------------------|--------|--------|--------| | ||
| ex1-programmerFun | 5 | - | ✓ | | ||
| ex2-pokemonApp | 5 | - | ✓ | | ||
| ex3-rollAnAce | 7 | - | ✓ | | ||
| ex4-diceRace | 7 | - | ✓ | | ||
| ex5-vscDebug | - | - | ✓ | | ||
| ex6-browserDebug | - | - | ✓ | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// 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": "node", | ||
"request": "launch", | ||
"name": "Launch Program", | ||
"skipFiles": [ | ||
"<node_internals>/**" | ||
], | ||
"program": "${workspaceFolder}\\3-UsingAPIs\\Week2\\assignment\\ex5-vscDebug.js" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,18 +21,73 @@ Use async/await and try/catch to handle promises. | |
Try and avoid using global variables. As much as possible, try and use function | ||
parameters and return values to pass data back and forth. | ||
------------------------------------------------------------------------------*/ | ||
function fetchData(/* TODO parameter(s) go here */) { | ||
// TODO complete this function | ||
async function fetchData(url) { | ||
const getData = await fetch(url); | ||
if (!getData.ok) { | ||
throw new Error('Invalid URL'); | ||
} | ||
|
||
return getData.json(); | ||
} | ||
|
||
function fetchAndPopulatePokemons(/* TODO parameter(s) go here */) { | ||
// TODO complete this function | ||
function fetchAndPopulatePokemons(data) { | ||
const pokemonList = data.results; | ||
|
||
let selector = document.querySelector('select'); | ||
if (!selector) { | ||
selector = document.createElement('select'); | ||
document.body.appendChild(selector); | ||
} | ||
|
||
pokemonList.map((pokemon) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of the |
||
const createOption = document.createElement('option'); | ||
createOption.textContent = pokemon.name; | ||
createOption.value = pokemon.name; | ||
selector.appendChild(createOption); | ||
}); | ||
|
||
selector.addEventListener('change', async () => { | ||
try { | ||
const data = await fetchData( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer if fetching the Pokémon image was handled within the |
||
`https://pokeapi.co/api/v2/pokemon/${selector.value}` | ||
); | ||
fetchImage(data); | ||
} catch (error) { | ||
throw new Error(error); | ||
} | ||
}); | ||
|
||
document.querySelector('button').style.display = 'none'; | ||
} | ||
|
||
function fetchImage(/* TODO parameter(s) go here */) { | ||
// TODO complete this function | ||
async function fetchImage(data) { | ||
const pokemonImage = data.sprites.other.home.front_default; | ||
|
||
let imgContainer = document.querySelector('#img-container'); | ||
if (!imgContainer) { | ||
imgContainer = document.createElement('img'); | ||
imgContainer.id = 'img-container'; | ||
document.body.appendChild(imgContainer); | ||
} | ||
|
||
imgContainer.src = pokemonImage; | ||
} | ||
|
||
function main() { | ||
// TODO complete this function | ||
const createButton = document.createElement('button'); | ||
createButton.textContent = "Catch 'em all"; | ||
document.body.appendChild(createButton); | ||
|
||
createButton.addEventListener('click', async () => { | ||
try { | ||
const data = await fetchData( | ||
'https://pokeapi.co/api/v2/pokemon?limit=151' | ||
); | ||
fetchAndPopulatePokemons(data); | ||
} catch (error) { | ||
throw new Error(error); | ||
} | ||
}); | ||
} | ||
|
||
window.addEventListener('load', main); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
/* add your styling here */ | ||
body { | ||
display: flex; | ||
flex-flow: column nowrap; | ||
align-items: center; | ||
justify-content: center; | ||
height: 100dvh; | ||
} | ||
|
||
button { | ||
width: 300px; | ||
padding: 10px 30px; | ||
background-color: white; | ||
border: 3px solid #3363af; | ||
border-radius: 5px; | ||
font-size: 24px; | ||
font-weight: bold; | ||
color: #3363af; | ||
cursor: pointer; | ||
transition: all 0.3s ease; | ||
} | ||
|
||
button:hover { | ||
background-color: #3363af; | ||
color: #fff; | ||
} | ||
|
||
select { | ||
padding: 10px; | ||
width: 300px; | ||
margin: 20px; | ||
border: 3px solid #3363af; | ||
border-radius: 5px; | ||
font-size: 24px; | ||
font-weight: bold; | ||
color: #3363af; | ||
cursor: pointer; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*** Unit Test Error Report *** | ||
|
||
PASS .dist/3-UsingAPIs/Week2/unit-tests/ex1-programmerFun.test.js (7.498 s) | ||
api-wk2-ex1-programmerFun | ||
✅ HTML should be syntactically valid (84 ms) | ||
✅ should have all TODO comments removed (1 ms) | ||
✅ should use `fetch()` | ||
✅ should use async/wait | ||
✅ should use try/catch | ||
|
||
Test Suites: 1 passed, 1 total | ||
Tests: 5 passed, 5 total | ||
Snapshots: 0 total | ||
Time: 7.842 s | ||
Ran all test suites matching /C:\\Users\\zet_a\\Desktop\\HYF\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week2\\unit-tests\\ex1-programmerFun.test.js/i. | ||
No linting errors detected. | ||
No spelling errors detected. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*** Unit Test Error Report *** | ||
|
||
PASS .dist/3-UsingAPIs/Week2/unit-tests/ex2-pokemonApp.test.js | ||
api-wk2-ex2-pokemonApp | ||
✅ HTML should be syntactically valid (78 ms) | ||
✅ should have all TODO comments removed | ||
✅ should use `fetch()` | ||
✅ should use `await fetch()` | ||
✅ should use try/catch | ||
|
||
Test Suites: 1 passed, 1 total | ||
Tests: 5 passed, 5 total | ||
Snapshots: 0 total | ||
Time: 2.571 s, estimated 3 s | ||
Ran all test suites matching /C:\\Users\\zet_a\\Desktop\\HYF\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week2\\unit-tests\\ex2-pokemonApp.test.js/i. | ||
No linting errors detected. | ||
No spelling errors detected. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
*** Unit Test Error Report *** | ||
|
||
PASS .dist/3-UsingAPIs/Week2/unit-tests/ex3-rollAnAce.test.js | ||
api-wk2-ex3-rollAnAce | ||
✅ should have all TODO comments removed (1 ms) | ||
✅ `rollDieUntil` should not contain unneeded console.log calls | ||
✅ should not include a recursive call | ||
✅ should use async/wait | ||
✅ should use try/catch | ||
✅ should resolve as soon as a die settles on an ACE (170 ms) | ||
✅ should reject with an Error when a die rolls off the table (126 ms) | ||
|
||
Test Suites: 1 passed, 1 total | ||
Tests: 7 passed, 7 total | ||
Snapshots: 0 total | ||
Time: 0.702 s, estimated 1 s | ||
Ran all test suites matching /C:\\Users\\zet_a\\Desktop\\HYF\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week2\\unit-tests\\ex3-rollAnAce.test.js/i. | ||
No linting errors detected. | ||
No spelling errors detected. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
*** Unit Test Error Report *** | ||
|
||
PASS .dist/3-UsingAPIs/Week2/unit-tests/ex4-diceRace.test.js | ||
api-wk2-ex4-diceRace | ||
✅ should exist and be executable (1 ms) | ||
✅ should have all TODO comments removed | ||
✅ `rollDice` should not contain unneeded console.log calls (1 ms) | ||
✅ should use `dice.map()` | ||
✅ should use `Promise.race()` | ||
✅ should resolve as soon as a die settles successfully (6 ms) | ||
✅ should reject with an Error as soon as a die rolls off the table (80 ms) | ||
|
||
Test Suites: 1 passed, 1 total | ||
Tests: 7 passed, 7 total | ||
Snapshots: 0 total | ||
Time: 0.711 s | ||
Ran all test suites matching /C:\\Users\\zet_a\\Desktop\\HYF\\Assignments-cohort52\\.dist\\3-UsingAPIs\\Week2\\unit-tests\\ex4-diceRace.test.js/i. | ||
No linting errors detected. | ||
No spelling errors detected. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There could also be other errors - for example, the API endpoint could be unresponsive. A better handling of the error would be logging the details of the
response
object.