forked from amineaouini/challenge-3-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
184 lines (135 loc) · 6.04 KB
/
main.js
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
// 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
}
}
// clouser to return the name
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 employee(name, salary) {
function Name() {
return 'your name is : ' + name;
}
return Name();
}
var sayName = function() {
return 'your name is : ' + name;
}
var sayHello = function() {
return 'Hello : ' + 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
//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 employee(name, salary) {
var incSalary = salary;
function increes(amont) {
incSalary = incSalary + amont;
return incSalary;
}
return increes();
}
//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.
// 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"
//=============================================================================
/* 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.
// 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
// pet1.addInfo(age, owner, gender, species);
// c- create another function to increase the pet age by n value.
// 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
// 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 CreatsPets(name) {
var creat = '';
creat.name = name;
creat.getName = getName;
}
var getName = function() {
var that = this;
this.name = this.name;
return this.name;
}
var addInfo = function(age, owner, gender, species) {
return that.name + 'it age is ' + age + ' and his owner is '+ owner + ' and gender is ' + gender ;
}
// Now, to make sure that you are actually reading, make a comment below this and type: 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 maximum(numbers) {
return reduce(numbers, function(max, element,i){
if( max < element ) {
max = element;
}
return max;
});
}
//================================================================================
/* 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 :))