forked from BushraAlabsi/Toy_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHigherFunctionsEach2.js
More file actions
58 lines (53 loc) · 1.39 KB
/
Copy pathHigherFunctionsEach2.js
File metadata and controls
58 lines (53 loc) · 1.39 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
/*
using improved each create function that returns an array with all the names in the array
if the index is even
var x = [{name : 'Jon',age : 45}, {name : 'Ali', age : 28},
{name :'Omar', age :17},{name :'Ola', age :37}, {name 'Salwa', age : 22}];
pName(x); = > ['Jon', 'Omar', 'salwa']
*/
function each(array, func) {
for (var i = 0; i < array.length; i++) {
func(array[i], i);
}
}
function pName(argument) {
var allNames = {}
each(argument , function (argument,i) {
if(i %2 === 0 ){
allNames[i] = argument["name"]
}
})
return allNames ;
}
/*
1) using improved each with objects, create function that print every element inside the object
var obj_2 = {name: 'Ibrahim', age : 67, phone : '078-0000000'}
printValue(obj_2); =>
Ibrahim
67
078-0000000
var obj_1 = {name: 'Salim', age : 15, phone : '079-0000000'}
printValue(obj_1); =>
Salim
15
079-0000000
*/
function each(coll, func) {
if (Array.isArray(coll)) {
for (var i = 0; i < coll.length; i++) {
func(coll[i], i);
}
}
else {
for (var key in coll) {
func(coll[key], key);
}
}
}
function printValue(obj) {
var newArr = ""
each(obj , function (obj , key ) {
newArr += obj + "\n"
})
return newArr
}