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

Skip to content

Commit e78f012

Browse files
committed
Homework week2
1 parent 9ee553a commit e78f012

File tree

1 file changed

+110
-153
lines changed

1 file changed

+110
-153
lines changed

JavaScript1/Week2/homework.md

Lines changed: 110 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -6,200 +6,157 @@
66
- Go through the review of [last week](../Week1/review.md) (Work in progress, update this week :wrench:)
77
- Go through the review of [this week](../Week2/review.md) (work in progress, update this week :nut_and_bolt:)
88

9-
## Step 2: JavaScript
10-
> For all the following exercises create a new .js file. Try to find a proper name for each file or make a small comment about what it does inside for future reference
119

12-
1. Create a function that takes 3 arguments and returns the sum of the three arguments.
10+
## Step 2: Javascript warmup part one
11+
Just like last time, lets warmup our brain!
12+
Go back to FreeCodeCamp, start where you left of and finish the rest of the Basic JavaScript challenges. We know this seems like a lot, but the tasks are not so big, so hang in there!
1313

14-
2. Create a function named `colorCar` that receives a color, and prints out, "a red car" for example. (Hint: put it in your file and run it like before.)
14+
Please make sure you REALLY understand the exercises below:
15+
16+
- https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/store-multiple-values-in-one-variable-using-javascript-arrays/
17+
- https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/build-javascript-objects/
18+
- https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/add-new-properties-to-a-javascript-object/
1519

16-
3. Create an object and a function that takes the object as a parameter and prints out all of its names and values.
1720

18-
4. Create a function named `vehicleType` that receives a color, and a code, 1 for car, 2 for motorbike. And prints "a blue motorbike" for example when called as `vehicleType("blue", 2)`
21+
## Step 3: Javascript warmup part two
1922

20-
5. Can you write the following without the `if` statement, but with just as a single line with `console.log(...);`?
23+
1. Create a function that returns an empty object.
24+
2. Create a new variable called timeObject and assign it to the result of calling the previous function
25+
3. Inside the function, add a key to the object called time with the value of the number 12
26+
4. Log out timeObject. It should look like this:
2127
```js
22-
if (3 == 3) {
23-
console.log("true")
24-
} else {
25-
console.log("false")
28+
{
29+
time: 12,
2630
}
2731
```
2832

29-
6. Create a function called `vehicle`, like before, but takes another parameter called age, so that `vehicle("blue", 1, 5)` prints "a blue used car"
30-
31-
7. Make a list of vehicles, you can add `"motorbike"`, `"caravan"`, `"bike"`, or more.
32-
33-
8. How do you get the third element from that list?
34-
35-
9. Change the function `vehicle` to use the list of question 4. So that `vehicle("green", 3, 1)` prints "a green new caravan".
36-
37-
10. Use the list of vehicles to write an advertisement. So that it prints something like: `"Amazing Joe's Garage, we service cars, motorbikes, caravans and bikes."`. (Hint: use a `for` loop.)
38-
39-
11. What if you add one more vehicle to the list, can you have that added to the advertisement without changing the code for question 7?
40-
41-
12. Create an empty object
42-
43-
13. Create a function that takes two objects as parameters and compares them. You will actually need to write two functions — one that compares with `==` and one that compares with `===`. Remember that objects can have objects inside of them so you'll need to find a way to compare every element of every object (types and values). For example:
33+
---
4434

35+
1. Create a function that returns an array with with 3 empty objects.
36+
2. Call the function and assign the return of the function to a variable called colorObjects (like we did in the previous exercise)
37+
3. Change the array so that all the objects have one key called color. Make every object have a different color
38+
4. Log out colorObjects
39+
5. Give the function 3 parameters called color1, color2 and color3. The parameters should control the color of the 3 objects. color1 sets the color of the first object and so on.
40+
6. Call the function with 3 colors and log out colorObjects. colorObjects should look like this:
4541
```js
46-
var obj1 = {
47-
a: 1,
48-
b: "this is the letter b",
49-
c: {
50-
foo: "what is a foo anyway",
51-
bar: [1, 2, 3, 4]
52-
}
53-
};
54-
55-
var obj2 = {
56-
a: "1",
57-
b: "this is the letter b",
58-
c: {
59-
foo: "what is a foo anyway",
60-
bar: [1, 2, 3, 4]
61-
}
62-
};
42+
[{
43+
color: 'red',
44+
},{
45+
color: 'blue',
46+
},{
47+
color: 'yellow',
48+
}]
6349
```
50+
If the colors you called the function with were red, blue and yellow.
6451

65-
In our example we'll say that `obj1 == obj2` is `true` and `obj1 === obj2` is `false`. Make sure you can see why before you write any code!
66-
67-
Note: give this exercise your best shot but don’t spend more than, say, one hour on it.
52+
7. For every object in the array log out the color of the object. Hint use a for loop and read [Here](https://github.com/HackYourFuture/fundamentals/blob/master/fundamentals/objects.md) to check how to get the value of an object.
6853

69-
14. We saw that we can pass functions as arguments to other functions. Your task is to write a function that takes another function as an argument and runs it.
54+
---
7055

71-
```js
72-
function foo(func) {
73-
// What to do here?
74-
}
75-
76-
function bar() {
77-
console.log('Hello, I am bar!');
78-
}
79-
80-
foo(bar);
81-
```
56+
8. Today is Sunday and you ask your friend in how many days you are meeting. If the friend says
57+
I will see you in 9 days. The output should be Tuesday. You should get the today's day from the system.
58+
So typical console output is:
8259

60+
``Today is: Sunday``
61+
``How many days to meet : 9``
62+
``We are meeting on : Tuesday``
8363

84-
15. Write some code to test two arrays for equality using `==` and `===`. Test the following:
85-
86-
```js
87-
var x = [1, 2, 3];
88-
var y = [1, 2, 3];
89-
var z = y;
90-
```
64+
Hint: use remainder operator
9165

92-
What do you think will happen with `x == y`, `x === y` and `z == y` and `z == x`? Prove it!
93-
94-
> Don't cheat! Seriously - try it first.
95-
66+
## Step 3: Smart-ease - Goes Global!
67+
Following the massive success of Smart-ease's first couple of products things have been quite hectic at Smart-ease's shared office space. The phone is constantly ringing from journalists wanting to interview the genius behind the success and the greatest developers want to work for you. Wired wrote an article with the headline "Smart-ease as good as Smarties"
9668

97-
Check out this [Fiddle](http://jsfiddle.net/jimschubert/85M4z/). You need to open your browser’s Developer Tools to see the console output. Press the Run button in the upper right corner to run the code.
69+
BUT people are asking: What will be the next product that truly defines Smart-ease as the startup of the century?
9870

99-
More insights from this [Stack Overflow question](http://stackoverflow.com/questions/22395357/how-to-compare-two-arrays-are-equal-using-javascript).
71+
As always Smart-ease starts by helping people fix their problems. So lets venture out into the real world and see what we find:
10072

73+
### NOnoN0nOYes (Note taking app)
74+
> You are sitting at a meeting. A person is presenting some interesting thought that you want to write down. You check your bag, but realise that you forgot to bring your notepad. Hmm you check the internet for an online solution. But you need to signup for them all, too bad... Maybe this could be a worthwhile problem so solve. Lets create a simple and easy to use notepad 📝
10175
102-
16. Take a look at the following code:
76+
#### Save a note
77+
The first thing we will create is the functionality to save a note:
78+
Create a variable called notes and assign it to an empty array.
79+
Create a function called addNote. The addNote has two parameters: note and noteId. The note parameter is a string. and the noteId is a number.
80+
The addNote function shall push an object to the notes array. This object has two keys: note and noteId, that corresponds to the parameters. The value of the keys should be the value of the corresponding parameters.
10381

104-
```js
105-
var o1 = { foo: 'bar' };
106-
var o2 = { foo: 'bar' };
107-
var o3 = o2;
82+
#### Get a note
83+
Now a user can add a note, but what if a user wants to see a specific note, but only remembers the id? Lets create that functionality for him:
84+
Create a function called getNoteFromId. The function has one parameter called id. When calling this function with an id it should return the relevant note object that corresponds to the id. If no id is specified or if the id is not a number, log out an error string.
85+
Hint: Use a for loop.
10886

109-
```
87+
#### Read all notes
88+
Now a user can both add and get a note. What if the user just wants to read all his notes? Lets also create that functionality:
89+
Create a function showAllNotes. When calling it the function should log this string out for every note:
90+
"The note with id: 1, has the following note text: "some example note"."
11091

111-
Show that changing `o2` changes `o3` (or not) and changing ~~`o2` changes `o3`~~ `o1` changes `o3`(or not).
112-
113-
Does the order that you assign (`o3 = o2` or `o2 = o3`) matter? {Jim Cramer: ???}
92+
#### Unique feature
93+
Suddenly you get this great idea for making the note app even better!
11494

115-
17. What does the following code return? (And why?)
116-
```js
117-
let bar = 42;
118-
typeof typeof bar;
119-
```
95+
Come up with a unique feature you think would make this app better. Write down the idea and see if you can implement it. If it is too hard to implement try and ask in the slack channel :)
12096

121-
122-
> ‘Coerce' means to try to change - so coercing `var x = '6'` to number means trying to change the type to number temporarily.
97+
### CactusIO-interactive (Smart phone usage app)
98+
> After a long day you come home to relax. The first thing you do is find your phone and start watching some youtube. Then check facebook, and then reading some news. Suddently a hour has passed. What happened to all that time you think to yourself. Maybe we can create some program to help with this problem! What if we could help users manage their smart phone usage?
12399
100+
Its going to work like this: A user can add smartphone activities. Then he can see a status on how his smartphone usage is going.
124101

125-
## Step 3: **Finish basic freeCodeCamp challenges:**
126102

127-
Go back to FreeCodeCamp, start where you left of and finish the rest of the Basic JavaScript challenges.
103+
#### Adding an activity
104+
Lets create the first part of the functionality that is adding activities.
128105

129-
Please make sure you REALLY understand the exercises below:
130-
- https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/multiply-two-decimals-with-javascript/
131-
- https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/store-multiple-values-in-one-variable-using-javascript-arrays/
132-
- https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/build-javascript-objects/
133-
- https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/add-new-properties-to-a-javascript-object/
134-
- https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/delete-properties-from-a-javascript-object/
106+
Create a variable called activities that stores all activities of the day. What type of variable would you think would make sense?
107+
Create a function called addActivity. It should have three parameters: date, activity and duration. The date should be a string, the activity a string and the duration a number. To save the activity push an object that contains the date, the activity and the duration to the activities variable.
135108

109+
Now a user can add an activity by writing:
136110
```
137-
How to hand in your homework:
138-
• Upload your homework in your "hyf-javascript1" Github repository. Make sure to create a new folder "week2" first.
139-
• Upload your homework files inside the week2 folder and write a description for this “commit”.
140-
• Your hyf-javascript1/week2 should now contain all your homework files.
141-
• Place the link to your repository folder in Trello.
111+
addActivity('23/7-18', 'Youtube', 30);
112+
113+
/*
114+
activities should now look like this
115+
[{
116+
date: '23/7-18',
117+
activity: 'Youtube',
118+
duration: 30,
119+
}]
120+
*/
142121
```
143122

144-
:star: Additional resources and review: [here](/JavaScript1//Week2/REVIEW.md) (work in progress):star:
145-
146-
## Step 4
147-
148-
**1**
149-
Today is Sunday and you ask your friend in how many days you are meeting. If the friend says
150-
I will see you in 9 days. The output should be Tuesday.
123+
Just adding activities wont help the user very much, we need to add some functionality to show the user how his smart phone usage is going.
151124

152-
You should get the today's day from the system.
153-
154-
So typical console output is:
155125

126+
#### Show my status
127+
Create a function called ShowStatus. This function should use the activities variable and return a string saying the following:
128+
"You have added 3 activities. They amount to 78 min. of usage"
129+
```
130+
ShowStatus(activities); // will log out this "You have added 3 activities. They amount to 78 min. of usage"
131+
```
132+
Now what happens if we call get status and activities is empty? We need to take that into consideration: If activities is empty log out a string that says: "Add some activities before calling ShowStatus"
156133

157-
``Today is: Sunday``
158-
``How many days to meet : 9``
159-
``We are meeting on : Tuesday``
160-
161-
162-
163-
Hint: ask each other for help/ask the right questions to the mentors.
164-
165-
**2**
166-
167-
1. Write a JavaScript program
168-
- Create an object of ``Students`` with properties: ``firstName``, ``lastName``, ``address``, ``telephone``, ``postCode``, ``course``, ``grade``.
169-
- Create array of objects of students that reflects your class. Give random grades to each students
170-
anything between 60 and 95. Makes sure students names reflects your class mates. Assume everybody
171-
lives in Copenhagen area.
172-
- Create a function for finding average class grade.
173-
- Create a function for finding student who has got the lowest grade. Put an encouraging message.
174-
- Create a function for for finding student who got the highest grade. Put a celebration message.
134+
#### Usage limit
135+
A user asks us if it is possible to set a limit for his smartphone usage. "Off course it is" we promptly reply!
136+
We need to store that limit somewhere, but where and what type should this be?
137+
So how should it work? When ShowStatus is called and the users usage is above the limit he set. Log out the following string: "You have reached your limit, no more smartphoning for you!"
175138

176-
2. Write a JavaScript program which iterates the integers from 1 to 1000. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz".
177-
For numbers which are multiples of both three and five print "FizzBuzz".
139+
Come up with one feature you think would be helpful for this program.
178140

179-
3. Write a JavaScript program to construct the following pattern, using a nested for loop. This might be bit of a challenge but read on nested for loop.
180-
````js
181-
*
182-
* *
183-
* * *
184-
* * * *
185-
* * * * *
186-
* * * * * *
187-
````
141+
Optional
142+
* Lets improve the addActivity, so that we dont need to specify the date, but the function automatically figures out what the date is. Check out this link: https://stackoverflow.com/a/34015511
143+
* Improve the ShowStatus function by only showing the number of actitivies for that specific day.
144+
* Create a function for calculating the activity a user has spent the most time on.
145+
---
188146

189147

190-
**3 (Challenge - only do this one once you have finished 1 and 2). Please work closely with your pair we identified in class.**
148+
## Step 4: Rover the Robot (Optional)
149+
Go and try out this cool game: [roverjs.com](http://roverjs.com), written by one of the HYF teachers from Amsterdam, Joost Lubach. There are different levels. Solve the chapter up until and with the variables chapter. There might be a small price for completing that!
191150

192-
I would strongly recommend play the game couple of times and adjust the step below if
193-
necessary. See also the flow chart we gave you.
151+
## Step 5: Hand in Homework:
152+
Steps:
153+
* Use the hyf-homework repo and add your homework files in the javascript/javascript1/week2 folder
154+
* If you dont have the repo, fork it from here: https://github.com/HackYourFuture-CPH/hyf-homework
155+
* Make your commits for the homework. Remember to seperate the code into meaningful commits!
156+
* Push the changes to github
157+
* To finish the homework:
158+
* Fill in the form for the relevant week. The link to the form can be found at the top of your classes slack channel.
159+
* Share the github link to your classes slack channel.
160+
---
194161

195-
1. Create an object of ``card``
196-
2. Create an object of ``player`` with the following : ``name``, ``age``, ``winner``
197-
3. Create an object for ``game`` with the following variables: array of four players and four arrays of cards.
198-
4. Each array belongs to one player.
199-
5. Create a function to distribute the cards to the four arrays.
200-
6. Take the next player in the array.
201-
7. Find cards with highest frequency.
202-
8. Go to the next player and see if the player has the card you want
203-
9. If it does continue and go to 7. If not go to 6.
204-
10. If at any point there is one happy family in one of the array change the winner key to "true"
205-
declare winner.
162+
And thats it your are done 🎉

0 commit comments

Comments
 (0)