-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
66 lines (54 loc) · 1.9 KB
/
map.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
var _ = {};
_.each = function (collection, iterator) {
// If collection is an array...
if (Array.isArray(collection)) {
// Iterate over each element of the array
for (var i = 0; i < collection.length; i++) {
// For each element, call iterator(value, key, collection)
iterator(collection[i], i, collection);
}
}
// If collection is an object...
else {
// Iterate over each property / key of the object
for (var key in collection) {
// For each property / key, call iterator(value, key, collection)
iterator(collection[key], key, collection);
}
}
};
// Return the results of applying an iterator to each element.
_.map = function (collection, iterator) {
var results = [];
// Iterate over the collection
_.each(collection, function (value, key, collection) {
// At each value of the collection, call iterator on the current value
// Save the resultant value in a results array
results.push(iterator(value));
});
// When iteration is complete, return an array of all resultant values
return results;
};
var test = [1,2,3];
var add3 = function (num) {
return num + 3;
};
// "Unit" tests
// console.log(_.map(test, add3)); // [4, 5, 6]
// console.log(test);
var testString = "hello world";
var answerString = "Hello World";
function LetterCapitalize(str) {
// First, identify the "words" of the sentence. Separated by spaces.
var words = str.split(' ');
// Iterate over the words collection
// Capitalize the first letter of each word in the collection
var capitalizedWords = _.map(words, function (word) {
return word[0].toUpperCase() + word.slice(1);
});
// When done iterating, return a new string with the modified words
return capitalizedWords.join(' ');
}
console.log(LetterCapitalize(testString)); // 'Hello World'
// "Unit" test
console.log(LetterCapitalize(testString) === answerString); // should return true if working