-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblemSet2.js
71 lines (58 loc) · 1.7 KB
/
problemSet2.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
console.log('Repeat:');
console.log('----------------');
function repeat(fn, n){
for (let i=0; i<n; i++){
console.log(fn());
}
}
function hello(){
return('Hello word');
}
function goodbye(){
return('Goobye world')
}
console.log(repeat(hello, 5));
console.log(repeat(goodbye, 5));
console.log('----------------');
console.log('Filtered Names:');
// Return only names that begin with 'R'
const myNames = ['Rich', 'Joe', 'Bhaumik', 'Ray'];
function filter(arr, fn) {
let newArray = [];
// arr.forEach(el=>{
// // if (el[0]==='R'){
// // newArray.push(el);
// // }
// })
for(let i=0; i<arr.length; i++){
if(fn(arr[i])) {
newArray.push(arr[i])
}
}
return newArray;
}
// console.log(filter(myNames, ))
const filteredNames = filter(myNames, function(name) {
// This is a "predicate function" - it's a function that only returns a boolean
return name[0] === 'R';
});
console.log(filteredNames) // => ['Rich', 'Ray']
console.log('----------------');
console.log('Hazard Alert:');
console.log('----------------');
function hazardWarningCreator(typeOfWarning){
let warningCounter = 0;
return function(location){
warningCounter+=1;
console.log(`DANGER! There is a ${typeOfWarning} hazard at ${location}!`);
console.log(`The ${typeOfWarning} hazard alert has triggered ${warningCounter} time(s) today!`);
}
}
const rocksWarning=hazardWarningCreator('Rocks on the Road');
const mudSlideWarning=hazardWarningCreator('Active mudslide zone');
const wildFireWarning=hazardWarningCreator('Uncontrolled wildfire zone');
rocksWarning('Main St');
rocksWarning('Smith Ave');
wildFireWarning(`Smokey's House`);
console.log(rocksWarning);
console.log('----------------');