-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinClass.js
More file actions
108 lines (85 loc) · 2.53 KB
/
inClass.js
File metadata and controls
108 lines (85 loc) · 2.53 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
function print(x) {
console.log(x);
}
function each(collection, func) {
if(Array.isArray(collection)) {
for(var i = 0; i < collection.length; i++) {
func(collection[i], i);
}
} else {
for(var key in collection) {
func(collection[key], key);
}
}
}
function filter(collection, predicate) {
var acc = [];
each(collection, function(value, index) {
if(predicate(value, index)) {
acc.push(value);
}
});
return acc;
}
// Use filter on the array of people above to return only the people older
// than 30.
var people = [
{name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26},
{name: {first: "Louis", last: "Reasoner"}, age: 21},
{name: {first: "Ben", last: "Bitdiddle"}, age: 34},
{name: {first: "Eva", middle: "Lu", last: "Ator"}, age: 40},
{name: {first: "Lem", middle: "E.", last: "Tweakit"}, age: 45}
];
// print(filter(people, function(person) {
// return person.age > 30;
// }));
// => [
// {name: {first: "Ben", last: "Bitdiddle"}, age: 34},
// {name: {first: "Eva", middle: "Lu", last: "Ator"}, age: 40},
// {name: {first: "Lem", middle: "E.", last: "Tweakit"}, age: 45}
// ]
// Use filter on the array of people above to return only the people
// whose first name starts with the letter l. HINT: You can use array
// access notation to get the first character of an array, e.g.:
function nameStartWith(array, letter) {
return filter(array, function(item) {
return item.name.first[0].toLowerCase() === letter.toLowerCase();
})
}
print(nameStartWith(people, "l"));
// Basic Requirements
// We've shown that we can write the function sum -- which sums an array of
// numbers -- using reduce like this (see slides for more):
function sum(numbers) {
return reduce(numbers, function(total, number) {
return total + number;
}, 0);
}
function reduce(array, f, start) {
var acc = start;
each(array, function(element) {
acc = f(acc, element);
});
return acc;print(product(numbers));
}
var numbers =[1,2,3,4]; //15
// Replace the ??? below to complete the implementation of product, that returns the product of an array of
// numbers.
function product(numbers) {
console.log("There is a total of " + numbers.length + " iterations")
return reduce(numbers, function(total,num) {
console.log("total :" + total);
console.log("num :" +num);
console.log("updated total :" + (total * num));
return total * num;
},1)
}
function product1(numbers) {
var total = 1;
each(numbers, function(num) {
total = total * num;
})
return total;
}
print(product(numbers));
print(product1(numbers));