Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70245f3

Browse files
authored
Merge pull request HackYourFuture-CPH#77 from HackYourFuture-CPH/week9-homework
Week9 homework
2 parents 20503ce + 6bb889b commit 70245f3

File tree

3 files changed

+78
-81
lines changed

3 files changed

+78
-81
lines changed

JavaScript3/Week8/homework.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Lets build a website where people can say thumbs up or thumbs down to diffeent t
6262

6363
If you go into the [homework folder](homework) there is some html, css and some js. If you clone down the javascript repo, then you can simply copy the files into your hyf-homework js3 week 2 folder.
6464

65-
You will be working in the main.js file.
65+
You will be working in the index.js file.
6666

6767
### Manually create functionality
6868
What we want to happen when a button is clicked is the following:
114 KB
Loading

JavaScript3/Week9/homework.md

Lines changed: 77 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,113 @@
11
# Homework Week 9
22

33
## 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.
55

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.
68

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 :)
911

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.
2713

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();
5819
```
5920

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`.
6044

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+
![Output example](assets/homework-movies.png)
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!
6353
```js
6454

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;
6959
}
7060
}
7161

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
8986
}
9087
}
9188

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]);
9491
```
9592

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.
9794

9895
### Part 1
99-
Create the functionality for the `createShoppingCart` factory functions.
96+
Create the functionality for the `ShoppingCart` class.
10097
- `addProduct` should add a product to the products array.
10198
- `removeProduct` should remove a product from the products array.
10299
- `getTotal` should get the total price of the products.
103100
- `renderProducts` should render the products to html. You decide what to show and how.
104101
- `getUser` should return a promise with the data from this api: https://jsonplaceholder.typicode.com/users/1
105102

106103
### 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.
108105

109106
### 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.
111108

112109
### 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.
114111

115112
## Feedback giving time!
116113
Find a student to give feedback using this site: https://hyf-peer-review.herokuapp.com/

0 commit comments

Comments
 (0)