-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmain.js
More file actions
178 lines (146 loc) · 5.25 KB
/
Copy pathmain.js
File metadata and controls
178 lines (146 loc) · 5.25 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
// Use the following helper functions in your solution
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 filter(array, predicate) {
var acc = [];
each(array, function(element, i) {
if (predicate(element, i)) {
acc.push(element);
}
});
return acc;
}
function map(array, func) {
var acc = [];
each(array, function(element, i) {
acc.push(func(element, i));
});
return acc;
}
// Remember to relax :)
//=======================================================================
/* Q1 */
//=======================================================================
/*
Depending on data modeling concept that you have learned:
Create a factory function called makeComputer that represents computers,
What different attributes computers may have?
Create two computers object from your factory function and save them in one array called computers!
Note: please write one or two lines here describing your solution.
*/
function makeComputer(type, color, weight) {
// TODO: Your code here
computer ={};
computer.type = type;
computer.color = color;
computer.weight = weight;
return computer;
}
var computer1 = makeComputer('HP','black','200g');
var computer2 = makeComputer('Dell','black','500g');
// Write a function displayComputer that can be used to display one computer.
function displayComputer(computer) {
// TODO: Your code here
for(var element in computer){
console.log(computer[element]);
}
}
//=============================================================================
/* Q2 */
//=============================================================================
/*
Write a function that takes an array of strings as a input
and returns an array of all of those strings, but transformed to upper case.
You can use toUpperCase method to convert a string to upper case.
Solve it using the most appropriate helper functions(each,map,filter).
Note: please write one or two lines here describing your solution.
var strArr = ['hello', 'world', 'whirled', 'peas'];
uppercaseAll(strArr); ==> [ 'HELLO', 'WORLD', 'WHIRLED', 'PEAS' ]
*/
function uppercaseAll(arrayOfStrings) {
// TODO: your code here
return map(arrayOfStrings ,function(element){
return element.toUpperCase();
})
}
// return every elemnt in the array after converted to upper Case.
//=============================================================================
/* Q3 */
//=============================================================================
/*
Write a function that takes array of objects as an input and returns an array
with only the countries that have a population higher than 500 million.
Solve it using one of the most appropriate helperthe helpers functions(each,map,filter).
highestPopulation(data); ==> [{country: "China", population: 1409517397},{country: "India", population: 1339180127}]
Note: please write one or two lines here describing your solution.
Here’s the data you’ll work with:
*/
var data = [
{
country: 'China',
population: 1409517397
},
{
country: 'India',
population: 1339180127
},
{
country: 'USA',
population: 324459463
},
{
country: 'Indonesia',
population: 263991379
}
];
function highestPopulation(arrayOfObjects) {
// TODO: your code here
return filter(arrayOfObjects , function(element) {
return element['population'] > 500000000;
})
}
//return the fitered value which is higher than 500m.
//=============================================================================
/* Q4 */
//=============================================================================
/*
Write a function halveAll that takes an array of numbers as a input
and returns an array of all of those numbers halved (divided by two).
Note: solve it using the most appropriate helper functions(each,map,filter)
var numsArray = [2, 6, 20, 8, 14];
halveAll(numsArray); ==> [ 1, 3, 10, 4, 7 ]
Note: please write one or two lines here describing your solution.
*/
function halveAll(numbers) {
// your code is here
return map(numbers , function(number) {
return number / 2;
})
}
// return the number /2 after each iteration of map function.
//=============================================================================
/* Q5 */
//=============================================================================
/*
Write a function called values that accepts an object as a parameter, and outputs an array of the object's values.
Solve it using one of the most appropriate helpers functions(each,map,filter).
values({first : 5, second: 'something' , third : 129}) ==> [5, 'something', 129];
Note: please write one or two lines here describing your solution.
*/
function values(obj) {
// TODO: your code here
return map(obj , function(element , key){
return element;
})
}
// return the element after each iteration.
//Good Luck :))