-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterators-map.js
97 lines (67 loc) · 3.27 KB
/
iterators-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
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
// Iterators: .map() Method
// creates a new array populated with the results of calling a provided function on every element in the calling array.
// Ex 1
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
// Create the secretMessage array below
const secretMessage = animals.map(el => {return el[0]});
console.log('======= Ex 1 =======')
console.log(secretMessage.join('')); // HelloWorld
/////////////////////////////////////////////////////
// Ex 2
const bigNumbers = [100, 200, 300, 400, 500];
// Create the smallNumbers array below
const smallNumbers = bigNumbers.map(num => {return num / 100});
// arrow func with curly brackets and return keyword
const smallNumbersConcise = bigNumbers.map(num => num / 100);
// concise body without the return keyword
const smallNumbersAnon = bigNumbers.map(function (num) { return num / 100; })
// anonymous function
// all 3 map methods above produce the same result:
console.log('======= Ex 2 =======')
console.log(smallNumbers) // [ 1, 2, 3, 4, 5 ]
console.log(smallNumbersConcise) // [ 1, 2, 3, 4, 5 ]
console.log(smallNumbersAnon) // [ 1, 2, 3, 4, 5 ]
/////////////////////////////////////////////////////
// Ex 3
// function, squareNums(), that takes in an array of numbers and, using .map(), returns an array with the square of each of the elements of that array.
const numbers = [2, 7, 9, 171, 52, 33, 14]
const toSquare = num => num * num
const squareNums = arr => arr.map(toSquare);
const squareNumsObject = numbers.map(num => num * num);
console.log('======= Ex 3 =======')
console.log(squareNums(numbers))
console.log('squareNums typeof?', typeof squareNums) // function
console.log('squareNums isArray?', Array.isArray(squareNums)) // false
console.log('squareNumsObject typeof?', typeof squareNumsObject) // object
console.log('squareNumsObject isArray?', Array.isArray(squareNumsObject)) // true
// use Array.isArray or Object.prototype.toString.call
// Array.isArray(value)
// to differentiate regular objects from arrays
/////////////////////////////////////////////////////
// Ex 4
// Write a function shoutGreetings() that takes in an array of strings and returns a new array. This new array should contain all the strings from the argument array but with capitalized letters and an exclamation point appended to the end: 'heya' will become 'HEYA!'
let upperBang = (el) => el.toUpperCase() + "!";
// const shoutGreetings = arr => arr.map(upperBang);
// const shoutGreetings = arr => arr.map(el => el.toUpperCase() + '!')
// const shoutGreetings = arr => {
// return arr.map(el => el.toUpperCase() + '!');
// }
// function shoutGreetings(arr){
// return arr.map(upperBang)
// }
// function shoutGreetings(arr){
// return arr.map(el => el.toUpperCase() + '!')
// }
const shoutGreetings = (arr) => {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
newArr.push(arr[i].toUpperCase() + "!");
}
return newArr;
};
const greetings = ["hello", "hi", "heya", "oi", "hey", "yo"];
console.log('======= Ex 4 =======')
console.log(shoutGreetings(greetings));
console.log('shoutGreetings isArray?', Array.isArray(shoutGreetings), '\ntypeof?', typeof shoutGreetings)
console.log('newArr typeof?', typeof newArr)
// Should print [ 'HELLO!', 'HI!', 'HEYA!', 'OI!', 'HEY!', 'YO!' ]