|
1 | 1 | # Homework Week 9
|
2 | 2 |
|
3 | 3 | ## So why should i do this homework?
|
4 |
| -Using constructor functions help with structuring code. It is one of many **design patterns**. The reason we use the constructr function design pattern is because it is a **solid desing pattern** first of. Second it **connects everything** you have learned in js: objects, function, properties, methods, keys, arrays. If you get how a constructor function work then you get how javascript work! |
| 4 | +Using classes help with structuring code. It is one of many **design patterns**. Second it **connects everything** you have learned in js: functions, properties, methods, keys, arrays. |
5 | 5 |
|
| 6 | +## Lets make some art using classes |
| 7 | +In HTML5 there is an elements called canvas. It works just like a canvas. You can paint elements to the canvas. |
6 | 8 |
|
7 |
| -## Step 2: Getting into promises |
8 |
| -Rewrite the following code (using promise and other control flow tools/features): |
| 9 | +### Paint a circle to a canvas element |
| 10 | +First add the canvas element to your html. Now draw a circle on the canvas using js. Google is your friend here :) |
9 | 11 |
|
10 |
| -```js |
11 |
| - function getAjaxData(url, callback) { |
12 |
| - const request = new XMLHttpRequest(); |
13 |
| - request.addEventListener('load', function () { |
14 |
| - if (this.status === 200) { |
15 |
| - callback(JSON.parse(request.responseText)); |
16 |
| - } else { |
17 |
| - console.log('Something is probably wrong with the url'); |
18 |
| - callback(null, true); |
19 |
| - } |
20 |
| - }); |
21 |
| - request.addEventListener('error', function () { |
22 |
| - callback(null, true); |
23 |
| - }); |
24 |
| - request.open("GET", url); |
25 |
| - request.send(); |
26 |
| - } |
| 12 | +When you have added a normal circle, try filling it out so it has a color. |
27 | 13 |
|
28 |
| - const usersURL = "https://jsonplaceholder.typicode.com/users"; |
29 |
| - |
30 |
| - getAjaxData(usersURL, function(data, err){ |
31 |
| - if(err) |
32 |
| - console.log('Error loading users: ', err); |
33 |
| - else { |
34 |
| - let users = data; |
35 |
| - for (var i = 0; i < users.length; i++) { |
36 |
| - // load the todos for this user |
37 |
| - const todosURL = `https://jsonplaceholder.typicode.com/users/${users[i].id}/todos`; |
38 |
| - |
39 |
| - // why is this line needed below? |
40 |
| - let index = i; |
41 |
| - |
42 |
| - getAjaxData(todosURL, function(data, err){ |
43 |
| - if(err) |
44 |
| - console.log('Error loading todos for user ', i, ' :', err); |
45 |
| - else{ |
46 |
| - users[index].todos = data; |
47 |
| - } |
48 |
| - |
49 |
| - // console.log(index, i); |
50 |
| - |
51 |
| - // if this is the last user, console.log all data |
52 |
| - if(index == users.length - 1) |
53 |
| - console.log(users); |
54 |
| - }); |
55 |
| - } |
56 |
| - } |
57 |
| - }); |
| 14 | +### Class creation time! |
| 15 | +Lets create a class called `Circle`. The circle should be used like this: |
| 16 | +```js |
| 17 | +const c1 = new Circle(50, 50, 20, 0, 2 * Math.PI, '#000000'); |
| 18 | +c1.draw(); |
58 | 19 | ```
|
59 | 20 |
|
| 21 | +Where the constructor should look like this: `constructor(x, y, r, startAngle, endAngle, fillColor)` |
| 22 | + |
| 23 | +The circle should have one method: `draw` that draws the circle to the canvas. |
| 24 | + |
| 25 | +Test if the new class works by creating a circle and drawing it to the canvas. |
| 26 | + |
| 27 | +### Now lets make art! |
| 28 | +Every half second create a new circle class and draw that to the canvas. |
| 29 | + |
| 30 | +The circle should have random `x`, `y`, `radius` and `color`. |
| 31 | + |
| 32 | +What if we wanted the canvas to have the same width and height of the screen? |
| 33 | + |
| 34 | +### Follow the mouse - optional |
| 35 | +Instead of the circles just randomly appearing on the screen, make them appear around the cursor. |
| 36 | + |
| 37 | + |
| 38 | +## Getting into promises |
| 39 | +Get `movies` using this api: https://gist.githubusercontent.com/pankaj28843/08f397fcea7c760a99206bcb0ae8d0a4/raw/02d8bc9ec9a73e463b13c44df77a87255def5ab9/movies.json |
| 40 | + |
| 41 | +Show the movie poster and the title for the first 20 movies from the above api. To get a movie poster for a movie, use this api: https://developers.themoviedb.org/3/getting-started/introduction You need to investigate a bit how to get the poster for a given movie. But help each other in the class and ask questions if you struggle too much. |
| 42 | + |
| 43 | +Think about `promise.all` and using chaining of `.then`. |
60 | 44 |
|
61 |
| -## Step 3: Shopping cart using factory functions |
62 |
| -Let's get a bit more into factory functions! |
| 45 | +Here is an example of how the output should be: |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +hint: This task is difficult, so find someone to work with! |
| 50 | + |
| 51 | +## Shopping cart using Classes |
| 52 | +Let's get a bit more into creating classes! |
63 | 53 | ```js
|
64 | 54 |
|
65 |
| -function createProduct(name, price) { |
66 |
| - return { |
67 |
| - name: name, |
68 |
| - price: price, |
| 55 | +class Product { |
| 56 | + constructor(name, price) { |
| 57 | + this.name = name; |
| 58 | + this.price = price; |
69 | 59 | }
|
70 | 60 | }
|
71 | 61 |
|
72 |
| -function createShoppingCart(products) { |
73 |
| - return { |
74 |
| - addProduct: function(product) { |
75 |
| - // Implement functionality here |
76 |
| - }, |
77 |
| - removeProduct: function(product) { |
78 |
| - // Implement functionality here |
79 |
| - }, |
80 |
| - getTotal: function() { |
81 |
| - // Implement functionality here |
82 |
| - }, |
83 |
| - renderProducts: function() { |
84 |
| - // Implement functionality here |
85 |
| - }, |
86 |
| - getUser: function() { |
87 |
| - // Implement functionality here |
88 |
| - } |
| 62 | + |
| 63 | +class ShoppingCart { |
| 64 | + constructor(products) { |
| 65 | + this.products = products; |
| 66 | + } |
| 67 | + |
| 68 | + addProduct(product) { |
| 69 | + // Implement functionality here |
| 70 | + } |
| 71 | + |
| 72 | + removeProduct(product) { |
| 73 | + // Implement functionality here |
| 74 | + } |
| 75 | + |
| 76 | + getTotal() { |
| 77 | + // Implement functionality here |
| 78 | + } |
| 79 | + |
| 80 | + renderProducts() { |
| 81 | + // Implement functionality here |
| 82 | + } |
| 83 | + |
| 84 | + getUser() { |
| 85 | + // Implement functionality here |
89 | 86 | }
|
90 | 87 | }
|
91 | 88 |
|
92 |
| -const flatscreen = createProduct('flat-screen', 5000); |
93 |
| -const shoppingCart = createShoppingCart([flatscreen]); |
| 89 | +const flatscreen = new Product('flat-screen', 5000); |
| 90 | +const shoppingCart = new ShoppingCart([flatscreen]); |
94 | 91 | ```
|
95 | 92 |
|
96 |
| -So we have two factory functions/ constructor function. `createProduct` creates products represented by objects. `createShoppingCart` creates a shopping cart also represented as an object. |
| 93 | +So we have two classes. `Product` represents products. `ShoppingCart` represents a shopping cart. |
97 | 94 |
|
98 | 95 | ### Part 1
|
99 |
| -Create the functionality for the `createShoppingCart` factory functions. |
| 96 | +Create the functionality for the `ShoppingCart` class. |
100 | 97 | - `addProduct` should add a product to the products array.
|
101 | 98 | - `removeProduct` should remove a product from the products array.
|
102 | 99 | - `getTotal` should get the total price of the products.
|
103 | 100 | - `renderProducts` should render the products to html. You decide what to show and how.
|
104 | 101 | - `getUser` should return a promise with the data from this api: https://jsonplaceholder.typicode.com/users/1
|
105 | 102 |
|
106 | 103 | ### Part 2
|
107 |
| -Use the `addProduct` and the `removeProduct` functions. Use the `getUser` function to get a user. When the user has been fetched. Show the total price and the username in html and render the products for the user. This should be in html, but you have to choose what to show! |
| 104 | +Try and call the `addProduct` and the `removeProduct` functions. Call the `getUser` function to get a user. When the user has been fetched. Render the total price of the Shoppingcar, the username and the products in the shopping cart. |
108 | 105 |
|
109 | 106 | ### Part 3
|
110 |
| -The `createProduct` factory function should get a method that can change the name of the product using the `this` keyword. |
| 107 | +The `Product` class function should get a method called `getPrice`. The function should have `currency` as a parameter. Depending on the currency return the correct price. Add 3 or more curriencies. Or use an api for getting the price dependent on a currency that getPrice uses. |
111 | 108 |
|
112 | 109 | ### Part 4, optional
|
113 |
| -Be creative! Create some cool/weird/quirky functionality of either the `createProduct` or the `createShoppingCart` factory function. |
| 110 | +Be creative! Create some cool/weird/quirky functionality of either the `Product` class or the `ShoppingCart` class. |
114 | 111 |
|
115 | 112 | ## Feedback giving time!
|
116 | 113 | Find a student to give feedback using this site: https://hyf-peer-review.herokuapp.com/
|
|
0 commit comments