Skip to content

Commit 8023b1b

Browse files
committed
JavaScript-II. Some stretch completed.
1 parent afae86b commit 8023b1b

4 files changed

Lines changed: 192 additions & 38 deletions

File tree

assignments/array-methods.js

Lines changed: 102 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// 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.
1+
// // 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

3-
// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
3+
// // Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
44

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},
@@ -53,31 +53,112 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5353
{"id":49,"first_name":"Bel","last_name":"Alway","email":"[email protected]","shirt_size":"S","company_name":"Voolia","donation":107},
5454
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];
5555

56-
// ==== Challenge 1: Use .forEach() ====
56+
// // ==== 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.
5858
let fullName = [];
59-
console.log(fullName);
59+
runners.forEach(function(runner){
60+
fullName.push(runner.first_name + ' ' + runner.last_name);
61+
})
62+
console.log(`Challenge 1 Output:\n ${fullName}`);
6063

61-
// ==== 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);
64+
// // // ==== Challenge 2: Use .map() ====
65+
// // // 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
66+
let allCaps = runners.map(function(runner){
67+
return runner.first_name.toUpperCase();
68+
})
6569

66-
// ==== 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 = [];
69-
console.log(largeShirts);
70+
console.log(`\nChallenge 2 Output:\n${allCaps}`);
7071

71-
// ==== Challenge 4: Use .reduce() ====
72-
// 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
73-
let ticketPriceTotal = [];
74-
console.log(ticketPriceTotal);
72+
// // // ==== Challenge 3: Use .filter() ====
73+
// // // 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
74+
let largeShirts = runners.filter(function(runner){
75+
if(runner.shirt_size == 'L'){
76+
return runner;
77+
}
78+
});
79+
let values = JSON.stringify(largeShirts);
7580

76-
// ==== 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.
7881

79-
// Problem 1
82+
console.log(`\nChallenge 3 Output:\n${values}`);
83+
84+
// // // ==== Challenge 4: Use .reduce() ====
85+
// // // 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
86+
let ticketPriceTotal = runners.reduce(function(acc,val){
87+
return acc + val.donation;
88+
},0)
89+
90+
91+
console.log(`\nChallenge 4 Output:\n${ticketPriceTotal}`);
92+
93+
94+
// // // ==== Challenge 5: Be Creative ====
95+
// // // 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.
96+
97+
// // // Problem 1 - We've been hacked! It was someone from an @example.com domain address, can you find it? Return array with the persons info where there are any @example.com addresses.
98+
99+
console.log('\nChallenge 5 - Problem 1 Output:')
100+
const addresses = runners.filter(function(runner){
101+
let domain = runner.email.split('@');
102+
if(domain.includes('example.com')){
103+
return true;
104+
}
105+
});
106+
107+
addresses.forEach(function(item){
108+
console.log(item);
109+
})
110+
111+
// // // Problem 2 - We need more sponsers. Put together a list of all the runners who donated 200 dollars or more. Then add up their total donations, log the list and total. We're going to try and throw them an appreciation party in the most public way possible. Just want to see if we can afford it.
112+
113+
const donors = runners.filter(function(donor){
114+
if(donor.donation >= 200){
115+
return true;
116+
}
117+
});
118+
const total = donors.reduce(function(acc, val){
119+
return acc + val.donation;
120+
},0);
121+
122+
console.log(`\nChallenge 5 - Problem 2 Output:`)
123+
124+
donors.forEach(function(donor){
125+
console.log(donor);});
126+
127+
console.log(`\nTotal Donations :$${total}`);
128+
129+
// // // Problem 3 - In preparation for the race, we want to try and oranize the runners at the starting line according to who passed the benchmark run or not. For each runner include an entry called 'benchmark' and state whether they passed or failed. Return the array and how many people passed the benchmark.
130+
131+
function includeBenchmark(){
132+
let value = Number(Math.random() * 1).toFixed();
133+
if(value == 1){
134+
return 'Passed';
135+
}
136+
else{
137+
return 'Failed';
138+
}
139+
}
140+
141+
const benchmarkers = runners.map(function(runner){
142+
143+
return Object.defineProperty(runner, 'benchmark',{
144+
value: includeBenchmark(),
145+
writable: false,
146+
enumerable: true
147+
})
148+
});
149+
150+
151+
function passers(arr = benchmarkers){
152+
let totalPassed = 0;
153+
arr.forEach(function(runner){
154+
if(runner.benchmark == 'Passed'){
155+
totalPassed++;
156+
}
157+
})
158+
return totalPassed;
159+
}
160+
161+
let newarray = JSON.stringify(benchmarkers);
162+
console.log(`Challenge 5 - Problem 3 Output:\n ${benchmarkers} \nThe total number of runners who passed is ${passers()}`);
80163

81-
// Problem 2
82164

83-
// Problem 3

assignments/callbacks.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,74 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2121
2222
*/
2323

24-
2524
function getLength(arr, cb) {
2625
// getLength passes the length of the array into the callback.
26+
let alength = arr.length;
27+
return cb(alength);
2728
}
29+
getLength(items, function(output){
30+
console.log(`getLength() output:\n${output}`);
31+
})
2832

2933
function last(arr, cb) {
3034
// last passes the last item of the array into the callback.
35+
let lastItem = arr.pop();
36+
return cb(lastItem);
3137
}
38+
last(items, function(output){
39+
console.log(`\nlast() output:\n${output}`);
40+
})
3241

3342
function sumNums(x, y, cb) {
3443
// sumNums adds two numbers (x, y) and passes the result to the callback.
44+
let sum = x + y;
45+
return cb(sum);
3546
}
47+
sumNums(4,5,function(output){
48+
console.log(`\nsumNums() output:\n${output}`);
49+
})
3650

3751
function multiplyNums(x, y, cb) {
3852
// multiplyNums multiplies two numbers and passes the result to the callback.
53+
let product = x * y;
54+
return cb(product);
3955
}
56+
multiplyNums(6,7,function(output){
57+
console.log(`\nmultiplyNums() output:\n${output}`);
58+
})
4059

4160
function contains(item, list, cb) {
4261
// contains checks if an item is present inside of the given array/list.
4362
// Pass true to the callback if it is, otherwise pass false.
63+
if(list.includes(item)){
64+
return cb(true);
65+
}
66+
else{
67+
return cb(false);
68+
}
4469
}
70+
contains('yo-yo',items,function(output){
71+
console.log(`\ncontains() output:\n${output}`);
72+
})
4573

4674
/* STRETCH PROBLEM */
4775

76+
const items2 = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'yo-yo'];
77+
4878
function removeDuplicates(array, cb) {
4979
// removeDuplicates removes all duplicate values from the given array.
5080
// Pass the duplicate free array to the callback function.
5181
// Do not mutate the original array.
82+
let holderArray=[];
83+
for(let i = 0; i < array.length; i++){
84+
if(!holderArray.includes(array[i])){
85+
holderArray[i] = array[i];
86+
};
87+
}
88+
return cb(holderArray);
5289
}
90+
91+
removeDuplicates(items2, function(output){
92+
console.log(`\nStretch output:\n${output}`);
93+
})
94+

assignments/closure.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3+
let array = [1,2,3,4,5];
4+
function closureLevelOne(){
5+
console.log(`Challenge 1 Output:\n-closureLevelOne() access: ${array}`);
6+
closureLevelTwo();
7+
function closureLevelTwo(){
8+
let total = array.reduce(function(acc, val){
9+
return acc + val;
10+
})
11+
console.log(`\n-closureLevelTwo access: Total = ${total}`);
12+
closureLevelThree();
13+
function closureLevelThree(){
14+
console.log(`\n-closureLevelThree access: Element 3 is ${array[3]}`);
15+
}
16+
}
17+
}
18+
closureLevelOne();
319

420

521
// ==== Challenge 2: Create a counter function ====
6-
const counter = () => {
22+
const counter = (x) => {
723
// Return a function that when invoked increments and returns a counter variable.
24+
x++;
25+
return x;
826
};
9-
// Example usage: const newCounter = counter();
10-
// newCounter(); // 1
27+
console.log(`\nChallenge 2 Output:\n${counter(1)}`);
28+
//Example usage: const newCounter = counter();
29+
//newCounter() // 1
1130
// newCounter(); // 2
1231

1332
/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
1433

1534
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
16-
const counterFactory = () => {
35+
const counterFactory = (x) => {
1736
// Return an object that has two methods called `increment` and `decrement`.
1837
// `increment` should increment a counter variable in closure scope and return it.
1938
// `decrement` should decrement the counter variable and return it.
39+
let counter = x;
40+
function increment(counter){
41+
counter++;
42+
return counter;
43+
};
44+
45+
function decrement(){
46+
counter--;
47+
return counter;
48+
};
49+
//console.log(counter);
2050
};
51+

assignments/stretch-function-conversion.js

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

3-
// let myFunction = function () {};
3+
let myFunction = () => {};
44

5-
// let anotherFunction = function (param) {
6-
// return param;
7-
// };
5+
let anotherFunction = (param) =>{
6+
return param;
7+
};
88

9-
// let add = function (param1, param2) {
10-
// return param1 + param2;
11-
// };
12-
// add(1,2);
9+
let add = (param1, param2) => {
10+
return param1 + param2;
11+
};
12+
add(1,2);
1313

14-
// let subtract = function (param1, param2) {
15-
// return param1 - param2;
16-
// };
17-
// subtract(1,2);
14+
let subtract = (param1, param2) => {
15+
return param1 - param2;
16+
};
17+
subtract(1,2);
1818

1919
// exampleArray = [1,2,3,4];
2020
// const triple = exampleArray.map(function (num) {

0 commit comments

Comments
 (0)