Skip to content

Commit dbb4e2f

Browse files
Merge pull request #1 from exploratum/thierry-joux
Thierry joux
2 parents 770541a + 344fce5 commit dbb4e2f

3 files changed

Lines changed: 105 additions & 13 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,42 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5454
{"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}];
5555

5656
// ==== Challenge 1: Use .forEach() ====
57-
// 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.
57+
// 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
58+
//called fullName.
59+
60+
console.log("********************Full Name Array***************************")
5861
let fullName = [];
62+
runners.forEach(runner=> fullName.push(`${runner.first_name} ${runner.last_name}`));
63+
5964
console.log(fullName);
6065

6166
// ==== Challenge 2: Use .map() ====
62-
// 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
63-
let allCaps = [];
64-
console.log(allCaps);
67+
// 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
68+
//caps and log the result
69+
70+
console.log("********************First Name capitalized***************************")
71+
// let allCaps = [];
72+
allCaps = runners.map(runner => runner.first_name.toUpperCase());
73+
console.log(allCaps);
6574

6675
// ==== Challenge 3: Use .filter() ====
67-
// 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
68-
let largeShirts = [];
76+
// 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
77+
//array named largeShirts that contains information about the runners that have a shirt size of L and log the result
78+
console.log("********************List of Large Size Shirt Runners***************************")
79+
largeShirts = runners.filter(runner => runner.shirt_size === 'L');
6980
console.log(largeShirts);
7081

7182
// ==== Challenge 4: Use .reduce() ====
7283
// 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
84+
console.log("********************Adding up all donations***************************")
7385
let ticketPriceTotal = [];
86+
ticketPriceTotal.push(runners.reduce((total, runner) => total + runner.donation, 0));
7487
console.log(ticketPriceTotal);
7588

89+
7690
// ==== Challenge 5: Be Creative ====
77-
// Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above.
91+
// Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme.
92+
//Try to create and then solve 3 unique problems using one or many of the array methods listed above.
7893

7994
// Problem 1
8095

‎assignments/callbacks.js‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,66 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2424
2525
*/
2626

27-
27+
/////////////////////////////////////////////////////////////
2828
function getLength(arr, cb) {
2929
// getLength passes the length of the array into the callback.
30+
return(cb(arr.length));
3031
}
3132

33+
getLength(items, function(length) {
34+
console.log(length);
35+
})
36+
37+
//////////////////////////////////////////////////////////////
3238
function last(arr, cb) {
3339
// last passes the last item of the array into the callback.
34-
}
40+
return(cb(arr[arr.length -1]));
41+
};
3542

43+
last(items, function(last) {
44+
console.log(last);
45+
});
46+
47+
//////////////////////////////////////////////////////////////
3648
function sumNums(x, y, cb) {
3749
// sumNums adds two numbers (x, y) and passes the result to the callback.
38-
}
50+
let sum = x+y;
51+
return(cb(sum));
52+
};
3953

54+
sumNums(2,3,function(sum){
55+
console.log(sum);
56+
});
57+
58+
//////////////////////////////////////////////////////////////
4059
function multiplyNums(x, y, cb) {
4160
// multiplyNums multiplies two numbers and passes the result to the callback.
61+
let product = x * y;
62+
return cb(product);
4263
}
4364

65+
multiplyNums(2,3, function(product) {
66+
console.log(product);
67+
})
68+
69+
//////////////////////////////////////////////////////////////
4470
function contains(item, list, cb) {
4571
// contains checks if an item is present inside of the given array/list.
4672
// Pass true to the callback if it is, otherwise pass false.
73+
let present = list.includes(item);
74+
return cb(present);
4775
}
4876

77+
contains('yo-yo', items, function(present) {
78+
console.log(present);
79+
});
80+
//////////////////////////////////////////////////////////////
4981
/* STRETCH PROBLEM */
5082

5183
function removeDuplicates(array, cb) {
5284
// removeDuplicates removes all duplicate values from the given array.
5385
// Pass the duplicate free array to the callback function.
5486
// Do not mutate the original array.
87+
88+
5589
}

‎assignments/closure.js‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,64 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
33

4+
function embellish() {
5+
let title = "simple title";
6+
7+
function formatter() {
8+
title = "@@@@@@@@@@@@@@@@ " + title + " @@@@@@@@@@@@@@@@@@";
9+
console.log(title);
10+
}
11+
return formatter;
12+
13+
}
14+
15+
innerFunction = embellish();
16+
innerFunction();
417

518
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
619

720

821
// ==== Challenge 2: Create a counter function ====
22+
23+
console.log("********************* STRETCH CHALLENGE: Increment a counter **********************")
924
const counter = () => {
1025
// Return a function that when invoked increments and returns a counter variable.
26+
let count = 0;
27+
function increment() {
28+
console.log(++count);
29+
}
30+
return increment;
1131
};
12-
// Example usage: const newCounter = counter();
13-
// newCounter(); // 1
14-
// newCounter(); // 2
32+
33+
newCounter = counter();
34+
//Example usage: const newCounter = counter();
35+
newCounter(); // 1
36+
newCounter(); // 2
37+
38+
1539

1640
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
41+
42+
console.log("********************* STRETCH CHALLENGE: Create object with increment and decrement functions **********************")
1743
const counterFactory = () => {
1844
// Return an object that has two methods called `increment` and `decrement`.
1945
// `increment` should increment a counter variable in closure scope and return it.
2046
// `decrement` should decrement the counter variable and return it.
47+
let count = 0;
48+
let methodObject = {
49+
increment: function() {console.log(++count);},
50+
decrement: function() {console.log(--count);}
51+
}
52+
return methodObject;
2153
};
54+
55+
let myObject = counterFactory();
56+
myObject.increment();
57+
myObject.increment();
58+
myObject.increment();
59+
myObject.decrement();
60+
myObject.increment();
61+
myObject.decrement();
62+
// myObject.increment();
63+
64+

0 commit comments

Comments
 (0)