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

Skip to content

Commit dad44b8

Browse files
authored
Merge pull request #72 from HackYourFuture-CPH/week-6-homework-change
change week6 homework
2 parents 8a23997 + fcbfeb1 commit dad44b8

File tree

7 files changed

+55
-100
lines changed

7 files changed

+55
-100
lines changed

JavaScript2/Week6/homework.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Homework Week 6
22

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

66
On top of that combining these array function with each other will show the functional side to javascript in a nice way.
77

@@ -10,7 +10,7 @@ The warmup exercises will be a bit abstract. But the in the hyfBay exercise the
1010
## Warmup array exercises
1111

1212
### 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**.
1414

1515
Your solution could be something like this:
1616
```js
@@ -26,28 +26,30 @@ for(let i = 0; i < numbers.length; i++) {
2626
console.log("The doubled numbers are", newNumbers); // [2, 6]
2727
```
2828

29-
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.
3030

3131
### 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:
3333
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*
3939

4040

4141
## 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.
4343

4444
### 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.
4646

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

4951
### 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:
5153
```html
5254
<li>
5355
<div class="name">Drone</div>
@@ -59,39 +61,42 @@ Call `getAvailableProducts` function to get the products array. Now add these pr
5961
```
6062

6163
### 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.
6365
A function is available to you called `sendPricesToServer`. You need to call this function with two arguments:
6466
1. An array of the prices of the products.
6567
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.
6668
Console.log out the response from the server.
6769

6870
### 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**.
7072

7173
### Filter using search.
7274
When the user writes something in the search input field. The products should be updated to only include the products that match the name.
7375

7476
### 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**.
7678

7779
### Sort the products - *optional*
7880
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.
7981

8082
### 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:
8284
```html
8385
<li>
8486
<div class="name">Drone</div>
8587
<div class="price">1234</div>
86-
</li>;
88+
</li>
8789
```
8890
Hint 1: Getting the object of the clicked product can be tricky! There are lots of ways but i will discuss two here:
8991
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.
9295

9396
### 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.
98+
99+
![Shopping cart](https://media.giphy.com/media/8PA8Ew3nw97yg/giphy.gif)
95100

96101
## Feedback giving time!
97102
Find a student to give feedback using this site: https://hyf-peer-review.herokuapp.com/
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Script loaded');

JavaScript2/Week6/homework/main.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

JavaScript2/Week6/readme.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ Functional composition
1717
* [Review](review.md)
1818

1919

20-
## Array methods filter, map and sort
20+
## Array methods map, filter and sort
2121

22-
### Filter
22+
### Map
2323

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."
2625

2726

2827
```js
@@ -46,6 +45,31 @@ const cars = [
4645
}
4746
];
4847

48+
// lets use the same cars array
49+
50+
// Lets transfrom it from an array of car objects to an array of car colors
51+
const carColors = cars.map(function(car) {
52+
return car.color;
53+
});
54+
55+
console.log(carColors); // ['lightYellow', 'lightBlue', 'lightCyan']
56+
57+
const carSpeeds = cars.map(function(car) {
58+
return car.speed;
59+
});
60+
61+
console.log(carSpeeds); // [45, 87, 78]
62+
```
63+
64+
65+
### Filter
66+
67+
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+
4973
// Lets get only cars with a speed larger than 60
5074
const fastCars = cars.filter(function(car) {
5175
// This function is called for every car in the cars array
@@ -63,27 +87,6 @@ console.log(fastCars); // logs the BMW and the Fiat
6387

6488

6589

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
75-
const carColors = cars.map(function(car) {
76-
return car.color;
77-
});
78-
79-
console.log(carColors); // ['lightYellow', 'lightBlue', 'lightCyan']
80-
81-
const carSpeeds = cars.map(function(car) {
82-
return car.speed;
83-
});
84-
85-
console.log(carSpeeds); // [45, 87, 78]
86-
```
8790

8891

8992
### Sort

0 commit comments

Comments
 (0)