-
Notifications
You must be signed in to change notification settings - Fork 71
Add prep exercises #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9774436
cat-walk exercises for week1 and week2
robvk facd23f
Added pokemon exercise
robvk f8c7874
Add text in MAKEME for prep exercises
robvk 3dccef4
Update Week1/prep-exercises/1-catwalk-promises/index.js
robvk 7663cc9
Update Week2/prep-exercises/2-pokemon-fetch/README.md
robvk b98854f
Merge branch 'main' into add-prep-exercises
robvk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Prep exercise - Cat walk with Promises | ||
|
|
||
| In the Browsers module you made an exercise to make a cat walk on the screen until it was halfway, then do a dance for 5 seconds after which it will continue walking until the end of the screen. In this exercise the result should be the same, but this time we want to use `Promise`s to write it in a different way. | ||
|
|
||
| Have a look [here](https://github.com/HackYourFuture/Homework/tree/main/2-Browsers/Week1#exercise-5-the-cat-walk) to remind yourself what the goal of the code was and then do the steps written in `index.js`. We have already provided a couple of functions for you. | ||
|
|
||
| ## Things to think about | ||
|
|
||
| - What do you think the advantages are of having the constants in the global scope? Are there any disadvantages? | ||
| - To make the code loop we cannot use a standard JavaScript loop (`for` or `while`). Why is that? | ||
| - Do you feel this version is easier to read than the version you made in the Browsers module? | ||
| - Is this version more efficient or not or does it not matter? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Cat Walk</title> | ||
| <style> | ||
| img { | ||
| position: absolute; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <img | ||
| src="http://www.anniemation.com/clip_art/images/cat-walk.gif" | ||
| alt="Cat walking" | ||
| /> | ||
| <script src="index.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 'use strict'; | ||
|
|
||
| const STEP_SIZE_PX = 10; | ||
| const STEP_INTERVAL_MS = 50; | ||
| const DANCE_TIME_MS = 5000; | ||
| const DANCING_CAT_URL = | ||
| 'https://media1.tenor.com/images/2de63e950fb254920054f9bd081e8157/tenor.gif'; | ||
|
|
||
| function walk(img, startPos, stopPos) { | ||
| return new Promise((resolve) => { | ||
| // Resolve this promise when the cat (`img`) has walked from `startPos` to | ||
| // `stopPos`. | ||
| // Make good use of the `STEP_INTERVAL_PX` and `STEP_INTERVAL_MS` | ||
| // constants. | ||
| }); | ||
| } | ||
|
|
||
| function dance(img) { | ||
| return new Promise((resolve) => { | ||
| // Switch the `.src` of the `img` from the walking cat to the dancing cat | ||
| // and, after a timeout, reset the `img` back to the walking cat. Then | ||
| // resolve the promise. | ||
| // Make good use of the `DANCING_CAT_URL` and `DANCE_TIME_MS` constants. | ||
| }); | ||
| } | ||
|
|
||
| function catWalk() { | ||
| const img = document.querySelector('img'); | ||
| const startPos = -img.width; | ||
| const centerPos = (window.innerWidth - img.width) / 2; | ||
| const stopPos = window.innerWidth; | ||
|
|
||
| // Use the `walk()` and `dance()` functions to let the cat do the following: | ||
| // 1. Walk from `startPos` to `centerPos`. | ||
| // 2. Then dance for 5 secs. | ||
| // 3. Then walk from `centerPos` to `stopPos`. | ||
| // 4. Repeat the first three steps indefinitely. | ||
| } | ||
|
|
||
| window.addEventListener('load', catWalk); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Prep exercise - Cat walk with async/await | ||
|
|
||
| In the previous week we changed the cat walk to an implementation using `Promise`s. Let's now use the new `async/await` syntax to simplify it a little more. Have a look at the `index.js` to see what to do. | ||
|
|
||
| The `dance` and `walk` functions are identical to last week, but our `catWalk` function can now be implemented using a standard `while` loop. Try to implement that and look at the following questions. | ||
|
|
||
| ## Things to think about | ||
|
|
||
| - Do you feel this version is easier to read than the version you made in the previous week? | ||
| - Is this version more efficient or not or is there no difference? | ||
| - Async/await makes the code wait until the Promise is resolved. Does this then also block any other functions from running? Why or why not? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Cat Walk</title> | ||
| <style> | ||
| img { | ||
| position: absolute; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <img | ||
| src="http://www.anniemation.com/clip_art/images/cat-walk.gif" | ||
| alt="Cat walking" | ||
| /> | ||
| <script src="index.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 'use strict'; | ||
|
|
||
| const STEP_INTERVAL_MS = 50; | ||
| const STEP_SIZE_PX = 10; | ||
| const DANCE_TIME_MS = 5000; | ||
| const DANCING_CAT_URL = | ||
| 'https://media1.tenor.com/images/2de63e950fb254920054f9bd081e8157/tenor.gif'; | ||
|
|
||
| function walk(img, startPos, stopPos) { | ||
| return new Promise((resolve) => { | ||
| // Copy over the implementation from last week | ||
| }); | ||
| } | ||
|
|
||
| function dance(img) { | ||
| return new Promise((resolve) => { | ||
| // Copy over the implementation from last week | ||
| }); | ||
| } | ||
|
|
||
| async function catWalk() { | ||
| const img = document.querySelector('img'); | ||
| const startPos = -img.width; | ||
| const centerPos = (window.innerWidth - img.width) / 2; | ||
| const stopPos = window.innerWidth; | ||
|
|
||
| // Use async/await syntax to loop the walk and dance functions | ||
| } | ||
|
|
||
| window.addEventListener('load', catWalk); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Prep exercise - Error handling | ||
|
|
||
| Until this week the code you have been working on is mostly your own and is generally very static which means that if it works once it will most likely keep working in the same way. When interacting with external API's over the internet this goes out the window. You have no control over the data the API gives and although usually fine, the internet is always going to be unreliable. This means your code needs to be able to handle it when something goes wrong and inform the user. | ||
|
|
||
| In this exercise we'll focus on the fetching and error handling part of working with an API, which you can use to work with any other code where a possible problem can occur. The `index.js` gives you instructions on what to do, there is some code there already, but feel free to alter that if needed. | ||
|
|
||
| The expected behaviour is as follows: | ||
|
|
||
| - When you press the **Get Data** button with the **Use invalid URL** checkbox **unchecked** the data from the Pokemon API will be fetched and rendered as JSON on the page. | ||
| - When you press the button with the checkbox **checked** an HTTP error message will be rendered on the page. | ||
| ## Things to think about | ||
|
|
||
| - If you look at the `index.html` you can see our error rendering is put into a regular `div` element, but our pokemon json is put into a `pre` element. Why is that? | ||
| - The comments say to handle the error in the main function. What do you think the advantages are of doing it this way? What about if you would do the error handling in the `fetchJSON` function? | ||
| - Some students ask us why not just put `try/catch` blocks around the main function and have that as the place to catch all errors. Why do you think we do not suggest doing this? | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <div> | ||
| <button type="button" id="button">Get Data</button> | ||
| <input type="checkbox" id="option" name="option" /> | ||
| <label for="option">Use invalid URL</label> | ||
| </div> | ||
| <div id="container"> | ||
| <pre id="json"></pre> | ||
| <div id="error"></div> | ||
| </div> | ||
| <script src="index.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| 'use strict'; | ||
| /*------------------------------------------------------------------------------ | ||
| * In this exercise you will practice fetching data from a web API, using | ||
| * `fetch`, promises, async/await and try/catch. | ||
| * | ||
| * Your solution should both work for the "happy" path (using VALID_URL) as | ||
| * well handle the error in the "unhappy" path (when selecting INVALID_URL). | ||
| * | ||
| * You will need to decide which functions need to be made `async` and where | ||
| * `try/catch` blocks should be added. | ||
| * | ||
| * The HTML file already contains the necessary HTML elements. | ||
| *----------------------------------------------------------------------------*/ | ||
|
|
||
| const VALID_URL = 'https://pokeapi.co/api/v2/pokemon/?limit=5'; | ||
| const INVALID_URL = 'https://pokeapi.co/api/v2/pokemons/?limit=5'; | ||
|
|
||
| async function fetchJSON(url) { | ||
| // TODO | ||
|
|
||
| // Fetch the JSON data from the web API that responds to the `url` parameter | ||
| // and return a promise that resolves to a corresponding JavaScript object. | ||
| // Make sure to check for HTTP errors. | ||
| } | ||
|
|
||
| function renderResults(pokemons) { | ||
| // 1. Clear the text content of the HTML element with id `error`. | ||
| const errorElement = document.querySelector('#error'); | ||
| errorElement.innerText = ''; | ||
|
|
||
| // 2. Set the text content of the HTML element with id `json` to JSON text | ||
| // from the `pokemons` argument, formatted in a human readable form (i.e., | ||
| // with indentation and line breaks). | ||
| const pokemonsElement = document.querySelector('#json'); | ||
| pokemonsElement.innerText = JSON.stringify(pokemons, null, 2); | ||
| } | ||
|
|
||
| function renderError(err) { | ||
| // 1. Clear the text content of the HTML element with id `json`. | ||
| const pokemonsElement = document.querySelector('#json'); | ||
| pokemonsElement.innerText = ''; | ||
|
|
||
| // 2. Set the text content of the HTML element with id `error` to the | ||
| // `.message` property of the `err` parameter. | ||
| const errorElement = document.querySelector('#error'); | ||
| errorElement.innerText = err; | ||
| } | ||
|
|
||
| function main() { | ||
| const button = document.querySelector('#button'); | ||
| button.addEventListener('click', () => { | ||
| const option = document.querySelector('#option'); | ||
| const url = option.checked ? INVALID_URL : VALID_URL; | ||
|
|
||
| // TODO | ||
| // Use `fetchJSON()` to fetch data from the selected url. | ||
| // If successful, render the data by calling function `renderResults()`. | ||
| // On failure, render the error by calling function `renderError()`. | ||
| }); | ||
| } | ||
|
|
||
| window.addEventListener('load', main); |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.