Skip to content

Commit d04a8dd

Browse files
Jamar TorresJamar Torres
authored andcommitted
finished callback.js file including the stretch challenge
1 parent 7d082ba commit d04a8dd

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

assignments/array-methods.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// A local community center is holding a fund rasising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
22

33
// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
4-
4+
console.log(`********* array-methods.js challenges *********`)
55
const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"[email protected]","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
66
{"id":2,"first_name":"Whitaker","last_name":"Ierland","email":"[email protected]","shirt_size":"2XL","company_name":"Wordtune","donation":148},
77
{"id":3,"first_name":"Julieta","last_name":"McCloid","email":"[email protected]","shirt_size":"S","company_name":"Riffpedia","donation":171},

assignments/callbacks.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
66

7+
8+
const items2 = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'Gum', 'yarn', 'yo-yo' ];
9+
// console.log(items2)
710
/*
811
912
//Given this problem:
@@ -61,32 +64,58 @@ multiplyNums(3, 4, function(mult){
6164
console.log("the multiplied value is: ", mult)
6265
})
6366

67+
// alternative way if multiplyNums has return cb(x,y)
68+
// function multiplying(x, y) {
69+
// console.log(x * y);
70+
// return x * y;
71+
// }
72+
73+
// multiplyNums(2, 4, multiplying)
74+
6475
function contains(item, list, cb) {
6576
// contains checks if an item is present inside of the given array/list.
6677
// Pass true to the callback if it is, otherwise pass false.
67-
for (let i = 0; i< item.length; i++){
68-
list = items.length;
69-
if (item[i] === list){
70-
debugger
71-
return true
72-
// return cb(item[i])
73-
debugger
74-
} else {
75-
return false;
76-
debugger
77-
}
78-
console.log(cb(item[i]))
79-
}
78+
return cb(item, list)
79+
// return cb(item, list)
80+
// console.log(cb(item, list))
8081
}
8182

82-
contains(items, function(checkItem){
83-
console.log("did this work? ", checkItem)
84-
})
83+
// researched "includes" method from MDN
84+
function inventory(item, list){
85+
return list.includes(item)
86+
}
87+
88+
89+
contains("Gum", items, inventory)
90+
console.log(contains("Gum", items, inventory))
91+
console.log(contains("Pen", items, inventory))
92+
93+
// contains("Gum", items, function(inventory){
94+
// console.log(inventory.includes)
95+
// })
96+
97+
// contains(items, "pencil", {
98+
// console.log("did this work? ", checkItem, itemListed)
99+
// })
85100

86101
/* STRETCH PROBLEM */
87102

88103
function removeDuplicates(array, cb) {
89104
// removeDuplicates removes all duplicate values from the given array.
90105
// Pass the duplicate free array to the callback function.
91106
// Do not mutate the original array.
107+
return cb(array)
92108
}
109+
110+
function removeThem(array){
111+
return array.filter(function(item, index){
112+
return array.indexOf(item) >= index;
113+
});
114+
}
115+
116+
removeDuplicates(items, removeThem)
117+
console.log(removeDuplicates(items, removeThem));
118+
119+
// created duplicate items array to check if it worked
120+
removeDuplicates(items2, removeThem)
121+
console.log("Removed duplicates from array of items2:", removeDuplicates(items2, removeThem))

assignments/closure.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ whoIsThatPokemon()
3636
// ==== Challenge 2: Create a counter function ====
3737
const counter = () => {
3838
let count = 0;
39-
console.log(count)
39+
console.log("default value of counter", count)
4040
// debugger
4141
// Return a function that when invoked increments and returns a counter variable.
4242
return () => {
4343
count ++;
44-
debugger
45-
console.log(count)
44+
// debugger
45+
console.log("closure function operated counter:", count)
4646
return count;
4747
}
4848
};

0 commit comments

Comments
 (0)