Skip to content

Commit 219c3f5

Browse files
committed
ARRAYS+CALLBACKS+CLOSURE+FUNC-CONVERSATION
1 parent f3e9281 commit 219c3f5

File tree

4 files changed

+106
-18
lines changed

4 files changed

+106
-18
lines changed

assignments/array-methods.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5555

5656
// ==== Challenge 1: Use .forEach() ====
5757
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName.
58+
5859
let fullName = [];
5960
runners.forEach( (val) =>{
6061
fullName.push(`${val.first_name} ${val.last_name} `)
@@ -65,33 +66,22 @@ console.log(fullName);
6566
// ==== Challenge 2: Use .map() ====
6667
// The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result
6768
let allCaps = [];
68-
runners.map( (val) =>{
69-
allCaps.push(`${(val.first_name).toUpperCase()} ${val.last_name} `)
70-
}
71-
)
69+
runners.map( (val) =>{ allCaps.push(`${(val.first_name).toUpperCase()} ${val.last_name} `)})
7270
console.log(allCaps);
7371

7472
// ==== Challenge 3: Use .filter() ====
7573
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
7674
let largeShirts = [];
77-
runners.filter( (val) =>{
78-
return (val.shirt_size === 'L' ? largeShirts.push(val) :false)})
75+
largeShirts = runners.filter( (val) =>{
76+
return val.shirt_size === 'L' })
7977

8078
console.log(largeShirts);
8179

8280
// ==== Challenge 4: Use .reduce() ====
8381
// The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result
8482
let ticketPriceTotal = [];
85-
let price = [];
86-
runners.map( (val) =>{
87-
price.push(val.donation)
88-
});
89-
90-
const x = price.reduce( (accumulator, nextvalue) =>{
91-
92-
return accumulator + nextvalue;
93-
});
94-
ticketPriceTotal.push(x)
83+
ticketPriceTotal = runners.reduce( (accumulator, nextvalue) =>{ return accumulator + nextvalue.donation; }, 0);
84+
9585
console.log( ticketPriceTotal);
9686

9787
// ==== Challenge 5: Be Creative ====

assignments/callbacks.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,91 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
22

33
function firstItem(arr, cb) {
44
// firstItem passes the first item of the given array to the callback function.
5+
return cb(arr) ;
56
}
7+
function getFirstItem (arr) {
8+
return arr[0];
9+
}
10+
console.log(firstItem(items, getFirstItem))
11+
12+
///////////////////////////////////////////////////////
13+
614

715
function getLength(arr, cb) {
816
// getLength passes the length of the array into the callback.
17+
return cb(arr) ;
918
}
19+
function lengthArr (arr) {
20+
return arr.length;
21+
}
22+
console.log(firstItem(items, lengthArr))
23+
24+
/////////////////////////////////
1025

1126
function last(arr, cb) {
1227
// last passes the last item of the array into the callback.
28+
29+
return cb(arr) ;
30+
}
31+
function getLastItem (arr) {
32+
return arr[arr.length - 1];
1333
}
34+
console.log(firstItem(items, getLastItem ))
35+
36+
///////////////////////////////////////
1437

1538
function sumNums(x, y, cb) {
1639
// sumNums adds two numbers (x, y) and passes the result to the callback.
40+
return cb(x, y);
41+
}
42+
function getSum (a, b) {
43+
return a + b ;
1744
}
45+
console.log(sumNums(150, 200, getSum))
46+
47+
///////////////////////////////////////////////
1848

1949
function multiplyNums(x, y, cb) {
2050
// multiplyNums multiplies two numbers and passes the result to the callback.
51+
return cb(x, y);
52+
}
53+
function getMultiply (a, b) {
54+
return a * b ;
2155
}
56+
console.log(multiplyNums(150, 200, getMultiply))
57+
58+
//////////////////////////////////////////////
2259

2360
function contains(item, list, cb) {
2461
// contains checks if an item is present inside of the given array/list.
2562
// Pass true to the callback if it is, otherwise pass false.
63+
return cb(item,list);
2664
}
65+
function getItemfromList(item, arr) {
66+
return arr.indexOf(item) !== -1 ? true : false ;
67+
}
68+
console.log(contains( 'hello', [1, 45, 5, 10, 'hello'], getItemfromList));
69+
2770

2871
/* STRETCH PROBLEM */
2972

3073
function removeDuplicates(array, cb) {
3174
// removeDuplicates removes all duplicate values from the given array.
3275
// Pass the duplicate free array to the callback function.
3376
// Do not mutate the original array.
77+
78+
return cb(array) ;
79+
80+
}
81+
82+
function noDuplicate (arr){
83+
let newArr = [];
84+
arr.forEach(function(item) {
85+
if(newArr.indexOf(item) < 0) {
86+
newArr.push(item)
87+
}
88+
89+
});
90+
return newArr;
3491
}
92+
console.log(removeDuplicates( [1, 45, 5, 10, 'hello', 14, 5, 1, 5, 10 ], noDuplicate))

assignments/closure.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1+
12
// ==== Challenge 1: Write your own closure ====
23
// Write a simple closure of your own creation. Keep it simple!
4+
function cspt2 (){
5+
let instructors = [ 'Dan', 'Moses', 'Raymond'];
6+
return {
7+
getInstructors : () => {
8+
return instructors;
9+
},
10+
addInstructor : (instructor) => {
11+
instructors.push(instructor);
12+
return instructors;
13+
}
14+
}
15+
}
16+
console.log(cspt2 ().getInstructors());
17+
console.log(cspt2 ().addInstructor ('NewInstructor'));
18+
319

420

521
// ==== Challenge 2: Create a counter function ====
622
const counter = () => {
723
// Return a function that when invoked increments and returns a counter variable.
24+
let count = 0;
25+
return () => {
26+
count ++
27+
return count;
28+
}
29+
830
};
31+
32+
console.log(counter ()());
33+
34+
935
// Example usage: const newCounter = counter();
1036
// newCounter(); // 1
1137
// newCounter(); // 2
12-
1338
/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
1439

1540
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====

assignments/function-conversion.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax
22

33
// let myFunction = function () {};
4+
let myFunction = () => {};
5+
46

57
// let anotherFunction = function (param) {
68
// return param;
79
// };
810

11+
let anotherFunction = (param) => param;
12+
13+
914
// let add = function (param1, param2) {
1015
// return param1 + param2;
1116
// };
1217
// add(1,2);
1318

19+
let add = (param1, param2) => param1 + param2;
20+
add(1,2);
21+
1422
// let subtract = function (param1, param2) {
1523
// return param1 - param2;
1624
// };
1725
// subtract(1,2);
1826

27+
let subtract = (param1, param2) => param1 - param2;
28+
subtract(1,2);
29+
1930
// exampleArray = [1,2,3,4];
2031
// const triple = exampleArray.map(function (num) {
2132
// return num * 3;
2233
// });
23-
// console.log(triple);
34+
// console.log(triple);
35+
36+
const exampleArray = [1,2,3,4];
37+
const triple = exampleArray.map( (num) => num * 3);
38+
console.log(triple);

0 commit comments

Comments
 (0)