forked from Ayaabuyousef/rbk-toy-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW5D3.js
More file actions
65 lines (60 loc) · 1.28 KB
/
W5D3.js
File metadata and controls
65 lines (60 loc) · 1.28 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
/*
1- Using map, Write a function called uppercaseValues that, given an object as a parameter,
returns a new object with all of its string values converted to uppercase.
Also, ensure that you only attempt to convert strings to uppercase.
*/
function each(coll,func){
if(Array.isArray){
for(var i = 0; i<coll.length;i++){
func(coll[i],i)
}
}else{
for(var key in coll){
func(coll[key],key)
}
}
}
function map(coll,f){
var acc = []
if(!Array.isArray(coll)){
acc = {}
}
each(coll,function (value,key){
acc[key] = f(value,key)
})
return acc
}
function reduce(arr, f, acc ) {
if (acc === undefined) {
acc = arr[0];
arr = arr.slice(1);
}
each(arr, function(element, i){
acc = f(acc, element, i);
});
return acc;
}
function uppercaseValues(obj){
return map(obj,function (element){
return element.toUpperValue
})
}
/*
2- Using reduce, write a function that sums the squared values of an array of numbers and returns the sum result.
*/
function reduce(arr, f, acc ) {
if (acc === undefined) {
acc = arr[0];
arr = arr.slice(1);
}
each(arr, function(element, i){
acc = f(acc, element, i);
});
return acc;
}
function sum(numbers) {
//your code is here
return reduce(numbers,function(result,element){
return result + element
},0)
}