forked from amineaouini/challenge-3-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
231 lines (152 loc) · 6.22 KB
/
Copy pathmain.js
File metadata and controls
231 lines (152 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// remember to relax and ask for help when you need it (only from staff)
// YOU CAN ONLY USE MDN AS A RESOURCE
// https://developer.mozilla.org/en-US/docs/Web/JavaScript
// NOTE: you are accountable for your styling, so make sure your styling is good.
// ANOTHER NOTE: please use the console to test your code :)
//==============================================================================
/* Q1 */
//==============================================================================
//lets make an employee profile using closures
function employee(name, salary) {
return {
name: name,
salary: salary
}
}
var employeeA = employee("jack", 100);
var employeeB = employee("Mark", 200);
var employeeC = employee("Sara", 150);
//create a function when invoked returns the name of that employee.
function sayMyName(){
var that=this;
return that.name
}
// employeeA.sayMyName(); // "jack"
// employeeB.sayMyName(); // "Mark"
//now modify that closure and add a function that says hello to the employee name;
// employeeA.sayHello(); // hello jack
// employeeB.sayHello(); // hello Mark
function sayHello(){
return "hello " + this.name
}
//modify your closure and add function increaseSalary that increases the salary for the employee by n value and return it.
//employeeA.increaseSalary(50); // "your salary is 150$"
function increaseSalary(n){
this.salary= this.salary+n;
return "your salary is "+this.salary
}
//how about we let jack and mark meet togther!
//modify your closure and add function addFriend that accepts an object as a parameter, and let jack meets his friends.
function addFreind(obj){
this.freinds = [],
this.friends.push(obj.name)
return "you just became friend with " + obj.name
}
// employeeA.addFriend(employeeB); // "you just became friend with Mark"
// employeeA.addFriend(employeeC); // "you just became friend with Mark and Sara"
//modify your closure to tell mark how many friends does he have.
// employeeA.listFriends(); // "you have 2 friends"
function listFriends(){
return "you have " + this.friends.length + "frinds"
}
//=============================================================================
/* Q2 */
//=============================================================================
//lets create a pet class using OOP concept,
// a - we need to create the pets (lets create only one for now), the invocation should take the name of the pet.
function Pet (name){
return{
name: name
}
}
// var pet1 = Pet("doggy");
// b - we need function to add the other info for the pet, called addInfo function. Make sure your functions unneeded memory space
function addInfo (age, owner, gender, species) {
var that=this
that.age=age;
that.owner=owner;
that.gender=gender;
that.species=species
}
// pet1.addInfo(age, owner, gender, species);
// c- create another function to increase the pet age by n value.
function increaseAge (n){
var that=this;
that.age=that.age+n;
}
// d - create a variable called availability with the default state as false, then create another function to check the pet state, returns true if the pet is available and false if it's not
function availibility (){
var that=this;
that.availibility= false;
}
function checkAvailibility (){
var that=this;
if (that.availibility){
return true;
}
return false
}
// f- in order to change the state of the pet, create a function called changeState, when called it will make the pet avaliablity true,
// and when called again it will make it false.
// Write your code here .....
function changeState (){
var that=this;
if (that.availibility){
that.availibility = false;
} else {
that.availibility = true
}
}
// Now, to make sure that you are actually reading, make a comment below this and type: Yes I am
//Yes I am
//=============================================================================
/* Q3 */
//=============================================================================
function each(coll, f) {
if (Array.isArray(coll)) {
for (var i = 0; i < coll.length; i++) {
f(coll[i], i);
}
} else {
for (var key in coll) {
f(coll[key], key);
}
}
}
function reduce(array, f, acc) {
if (acc === undefined) {
acc = array[0];
array = array.slice(1);
}
each(array, function (element, i) {
acc = f(acc, element, i);
});
return acc;
}
// Use the updated version of reduce to write a function max that returns the maximum number in an array of numbers.
// Write your code here .....
function max(acc, element, i){
if (element>acc){
return element
}
}
//================================================================================
/* Q4 */
//================================================================================
// you can only use MDN as a resource in case you need one (https://developer.mozilla.org/en-US/docs/Learn/HTML).
// 1-Create a new html file called html_yourname.html and do the following:
// a. Change the title to : My easy Assessment.
// d. Add horizital line.
// e. Create a new div with id myInfo.
// 1. Add header : HTML is Eazy
// 2. Add the following paragraph:
// HyperText Markup Language (HTML) is the standard markup language for creating web pages and web applications.
// f. create an input text and a button called Add.
// Create css file and link it to your HTML file, and write css code for the following:
// a. Change the background color for the whole page.
// b. Change the font family for the header of the page.
// c. Enlarge the input text (both: width & height).
// 2. Connect jQuery library to the HTML file.
// 3. Write javascript function when user type text inside the input text and click the "Add"
// button it will add the text to the ul element.
// Good Luck :))