You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: JavaScript2/Week6/homework.md
+26-21Lines changed: 26 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# Homework Week 6
2
2
3
3
## Why should i even do this homework?
4
-
Working with arrays is an essential part of being a javascript developer. A lot of the time js developers have an array of some objects. That could be users, products, posts, cars etc. Working with these arrays, js developers so often need to filter the arrays, change the structure of the array, sort them or loop through them.
4
+
Working with arrays is an essential part of being a javascript developer. A lot of the time js developers have an array of some objects. That could be **users, products, posts, jobs** etc. Working with these arrays, js developers so often need to filter the arrays, change the structure of the array, sort them or loop through them.
5
5
6
6
On top of that combining these array function with each other will show the functional side to javascript in a nice way.
7
7
@@ -10,7 +10,7 @@ The warmup exercises will be a bit abstract. But the in the hyfBay exercise the
10
10
## Warmup array exercises
11
11
12
12
### Doubling of number
13
-
Say you would like to write a program that doubles the odd numbers in an array and throws away the even number.
13
+
Say you would like to write a program that **doubles the odd numbers** in an array and **throws away the even number**.
14
14
15
15
Your solution could be something like this:
16
16
```js
@@ -26,28 +26,30 @@ for(let i = 0; i < numbers.length; i++) {
rewrite the above program using `map` and `filter` don't forget to use `=>`
29
+
Rewrite the above program using `map` and `filter` don't forget to use arrow functions.
30
30
31
31
### Working with movies
32
-
Copy the movies array in the movies.js file. Use this array to do the following tasks:
32
+
Copy the movies array in the [movies](homework/movies.js) file. Use this array to do the following tasks:
33
33
1. Count the number of movies made between 1980-1989 (including both the years).
34
-
1. Create a new array that has an extra key called tag. The tag is based in the rating: Good (>= 7), Average (>= 4 and < 7), Bad (< 4).
35
-
1. Using chaining, first map the movies array to only the rating of the movies. Now filter the movies array to only contain the movies rated higher than 6. Try do it where you define the functions as constants you provide to the filter and map functions.
36
-
1. Count he number of movies containing the following keywords: `["The", "dog", "who", "is", "not", "a", "man"]`. Can you make sure the search is case insensitive?
37
-
1. Calculate the average rating of all the movies using reduce. *Optional*
38
-
1. Count the total number of Good, Average and Bad movies using reduce. *Optional*
34
+
1. Create a new array that has an extra key called tag. The tag is based on the rating: Good (>= 7), Average (>= 4 and < 7), Bad (< 4).
35
+
1. Using chaining, first filter the movies array to only contain the movies rated higher than 6. Now map the movies array to only the rating of the movies. Try do it where you define the functions as constants you provide to the filter and map functions.
36
+
1. Count he number of movies containing the following keywords: `["The", "dog", "who", "is", "not", "a", "man"]`. Can you make sure the search is case insensitive? Hint: use indexOf
37
+
1. Calculate the average rating of all the movies using [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce). *Optional*
38
+
1. Count the total number of Good, Average and Bad movies using [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce). *Optional*
39
39
40
40
41
41
## hyfBay - get the okay'est products here
42
-
We have been hired for a company to do a SPA - Single Page App for them. It is a website where a user can add products to the users shopping cart. The products can be filtered and sorted based on what products the user wants to see.
42
+
We have been **hired for a company** to do a SPA - Single Page App for them. It is a website where a user can **add products to the users shopping cart**. The products can be **filtered and sorted** based on what products the user wants to see.
43
43
44
44
### Lets get started!
45
-
In the homework folder there is a project you should continue working on. So Copy the files into your hyf-homework week 6 folder and start working in the main.js file.
45
+
In the [homework/hyf-bay folder](homework/hyf-bay) there is a project you should continue working on. So copy all the files into your hyf-homework/Javascript/javascript2/week3 folder and start working in the main.js file.
46
46
47
-
There is a function available to you called `getAvailableProducts`. Calling this function will return an array of products. There can be from 0 - 30 products in the array. The products are represented by objects that has the following keys: `id`, `name`, `price`, `rating`, `shipsTo`.
47
+
I have done some rudimentary styling for the project but you are super welcome to make it even nicer!
48
+
49
+
There is a function available to you called `getAvailableProducts`. Calling this function will return an array of products. There can be from 0 - 30 products in the array. The products are represented by objects that has the following keys: `id`, `name`, `price`, `rating`, `shipsTo`.
48
50
49
51
### Add them products
50
-
Call `getAvailableProducts` function to get the products array. Now add these products (as li elements) to the `ul` under the section with the classname products. Here is an example of how the individual li element should look:
52
+
Call `getAvailableProducts` function to get the products array. Now add these products (as li elements) to the `ul` under the section with the classname `products`. Here is an example of how the individual li element should look:
51
53
```html
52
54
<li>
53
55
<divclass="name">Drone</div>
@@ -59,39 +61,42 @@ Call `getAvailableProducts` function to get the products array. Now add these pr
59
61
```
60
62
61
63
### Price analytics
62
-
In order for analysing the product prices we need to send the prices of the products to a server.
64
+
In order to analyse the product prices we need to send the prices of the products to a server.
63
65
A function is available to you called `sendPricesToServer`. You need to call this function with two arguments:
64
66
1. An array of the prices of the products.
65
67
2. A callback function with a parameter. The parameter will be the confirmation text sent from the server. The callback function will be called when the data has been sent to the server.
66
68
Console.log out the response from the server.
67
69
68
70
### Filter using countries
69
-
A user can filter the search result in different ways. One way is by choosing to only see products that ship from a certain country. This can be helpful for the user, so the user does not spend to much time looking for products that can not be shipped to the user. We need to create that functionality: When the user selects a country, the products should be updated with the products that ship to that country.
71
+
A user can filter the search result in different ways. One way is by choosing to only **see products that ship from a certain country**. This can be helpful for the user, so the user does not spend to much time looking for products that can not be shipped to the user. We need to create that functionality: When the **user selects a country**, the products should be updated with the **products that ship to that country**.
70
72
71
73
### Filter using search.
72
74
When the user writes something in the search input field. The products should be updated to only include the products that match the name.
73
75
74
76
### Create some extra feature
75
-
No matter how small or how big. Create some feature.
77
+
No matter how small or how big. Create some feature that would be **cool/helpful/quirky/funny**.
76
78
77
79
### Sort the products - *optional*
78
80
Choosing one of the sorting metods should update the shown order of the products. There is an array method called `sort`. Find documentation about it and see if you can get it to work.
79
81
80
82
### Shopping cart - *optional*
81
-
When clicking the Add to cart button for a product, that product should be added to the `ul` found under the section with the classname cart. The product should be added as a an `li` item. Like this:
83
+
When clicking the `Add to cart` button for a product, that product should be added to the `ul` found under the section with the classname `cart`. The product should be added as a an `li` item. Like this:
82
84
```html
83
85
<li>
84
86
<divclass="name">Drone</div>
85
87
<divclass="price">1234</div>
86
-
</li>;
88
+
</li>
87
89
```
88
90
Hint 1: Getting the object of the clicked product can be tricky! There are lots of ways but i will discuss two here:
89
91
1. Saving the id of the product in a data-id attribute on the button. When a user clicks the button you get the id of the product (from the data-id attribute). With the product id you can create a function that goes through all the products array and returns the product object with the corresponding id. Fx `getProductObjectFromId('Drone23576172')` would return the relevant object of the drone.
90
-
2. When adding the li tags to the ul. You can for every product in the products array create a new li element using `document.createElement('li')`. You then set the innerHTML as is specified above and then attach a clickListner: `createdLiElement.addEventListener('click', () => {});` The callback function has access to the product object because of something called `closures`. This approach i would say is the best way.
91
-
Hint 2: Have an array with the products in the shopping cart. Render the shopping cart using this array.
92
+
2. When adding the li tags to the ul. You can for every product in the products array create a new li element using `document.createElement('li')`. You then set the innerHTML as is specified above and then attach a clickListner: `createdLiElement.addEventListener('click', () => {});` The callback function has access to the product object because of something called [closures](https://www.youtube.com/watch?v=1JsJx1x35c0). This approach i would say is the best way.
93
+
94
+
Hint 2: Have an array with the products in the shopping cart. Render the shopping cart using this array. This will be super helpful if you do the next part.
92
95
93
96
### Total price of shopping cart - *optional*
94
-
Calculate the total price of all the elements in the shopping cart. Investigate the array function called `reduce`. It reduces an array down to one value. Which is exactly what we want. To reduce the products in the shopping cart array down to a single value that is the total price.
97
+
Calculate the total price of all the elements in the shopping cart. Investigate the array function called [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce). It reduces an array down to one value. Which is exactly what we want. To reduce the products in the shopping cart array down to a single value that is the total price.
Copy file name to clipboardExpand all lines: JavaScript2/Week6/readme.md
+28-25Lines changed: 28 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,12 +17,11 @@ Functional composition
17
17
*[Review](review.md)
18
18
19
19
20
-
## Array methods filter, map and sort
20
+
## Array methods map, filter and sort
21
21
22
-
### Filter
22
+
### Map
23
23
24
-
The filter function works on arrays and it filters out elements in our array.
25
-
It takes a function as input. This function is called on every element in the array. Just like forEach. If this function we create returns true the element is saved in the, if we return false the element is filtered out.
24
+
The map function works on arrays. It maps (think transform) the elements of an array. Or from MDN: "The map() method creates a new array with the results of calling a provided function on every element in the calling array."
26
25
27
26
28
27
```js
@@ -46,6 +45,31 @@ const cars = [
46
45
}
47
46
];
48
47
48
+
// lets use the same cars array
49
+
50
+
// Lets transfrom it from an array of car objects to an array of car colors
The filter function works on arrays and it filters out elements in our array.
68
+
It takes a function as input. This function is called on every element in the array. Just like forEach. If this function we create returns true the element is saved in the, if we return false the element is filtered out.
69
+
70
+
71
+
```js
72
+
49
73
// Lets get only cars with a speed larger than 60
50
74
constfastCars=cars.filter(function(car) {
51
75
// This function is called for every car in the cars array
@@ -63,27 +87,6 @@ console.log(fastCars); // logs the BMW and the Fiat
63
87
64
88
65
89
66
-
### Map
67
-
68
-
The map function works on arrays. It maps (think transform) the elements of an array. Or from MDN: "The map() method creates a new array with the results of calling a provided function on every element in the calling array."
69
-
70
-
71
-
```js
72
-
// lets use the same cars array
73
-
74
-
// Lets transfrom it from an array of car objects to an array of car colors
0 commit comments