Skip to content

Commit e3ffda2

Browse files
Merge pull request #1 from Dionuta/Dionuta-Burton
Dionuta burton
2 parents afae86b + b91593e commit e3ffda2

4 files changed

Lines changed: 100 additions & 27 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,64 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
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.
5858
let fullName = [];
59+
runners.forEach(function(a){
60+
fullName.push(`${a.first_name} ${a.last_name}`);
61+
});
5962
console.log(fullName);
6063

6164
// ==== Challenge 2: Use .map() ====
6265
// 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
6366
let allCaps = [];
67+
runners.map(function (firstName){
68+
allCaps.push(firstName.first_name.toUpperCase());
69+
});
6470
console.log(allCaps);
6571

6672
// ==== Challenge 3: Use .filter() ====
6773
// 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 = [];
74+
let largeShirts = runners.filter(function(runner){
75+
return runner.shirt_size == 'L';
76+
});
6977
console.log(largeShirts);
7078

7179
// ==== Challenge 4: Use .reduce() ====
7280
// 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 = [];
81+
let ticketPriceTotal = runners.reduce((accumulator, currentValue) => {
82+
return accumulator + currentValue.donation;
83+
}, 0); //The first time the function is called, it gets called with (0, {donation: 75 }) and returns 0 + 75 = 75. Then adds the rest the same way.
84+
7485
console.log(ticketPriceTotal);
7586

7687
// ==== Challenge 5: Be Creative ====
7788
// 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.
7889

7990
// Problem 1
91+
// If everyone had to pay cali taxes.
92+
93+
let taxes = [];
94+
95+
let caliTax = 1.0725;
96+
97+
runners.map((runner) => {
98+
taxes.push(Math.round((caliTax * runner.donation) * 100) /100);
99+
});
100+
101+
console.log(taxes);
80102

81103
// Problem 2
104+
//find the big donation
105+
const bigDonation = runners.filter(function (runner) {
106+
return runner.donation > 200;
107+
})
108+
109+
console.log(bigDonation);
110+
111+
// Problem 3
112+
113+
// company list
114+
let companyList = [];
115+
runners.forEach((i) => {
116+
return companyList.push(i.company_name);
117+
});
118+
console.log(companyList);
82119

83-
// Problem 3

‎assignments/callbacks.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,55 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2424

2525
function getLength(arr, cb) {
2626
// getLength passes the length of the array into the callback.
27+
return cb(arr.length);
2728
}
2829

30+
getLength(items, function(size) {
31+
console.log(size);
32+
})
33+
2934
function last(arr, cb) {
3035
// last passes the last item of the array into the callback.
36+
return cb(arr[arr.length-1]);
3137
}
3238

39+
last(items, function(lazt){
40+
console.log(lazt);
41+
})
42+
3343
function sumNums(x, y, cb) {
3444
// sumNums adds two numbers (x, y) and passes the result to the callback.
45+
return cb(x + y);
3546
}
3647

48+
sumNums(1, 89, function(sum){
49+
console.log(sum);
50+
})
51+
3752
function multiplyNums(x, y, cb) {
3853
// multiplyNums multiplies two numbers and passes the result to the callback.
54+
return cb(x * y);
3955
}
4056

57+
multiplyNums(85, 71, function(sum){
58+
console.log(sum);
59+
})
60+
4161
function contains(item, list, cb) {
4262
// contains checks if an item is present inside of the given array/list.
4363
// Pass true to the callback if it is, otherwise pass false.
64+
if (list.includes(item)){ /*The includes() method determines whether an array
65+
includes a certain value among its entries, returning true or false as appropriate.*/
66+
return cb(true);
67+
}
68+
return cb(false);
4469
}
4570

71+
contains('Gum', items, function(result){
72+
console.log(result);
73+
});
74+
75+
4676
/* STRETCH PROBLEM */
4777

4878
function removeDuplicates(array, cb) {

‎assignments/closure.js‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
function name(){
4+
let name = 'Dionuta';
5+
function addLastName () {
6+
const lastName = 'Burton'
7+
console.log(`${name} ${lastName}`)
8+
}
9+
addLastName();
10+
}
11+
name();
412

513
// ==== Challenge 2: Create a counter function ====
614
const counter = () => {
7-
// Return a function that when invoked increments and returns a counter variable.
8-
};
9-
// Example usage: const newCounter = counter();
10-
// newCounter(); // 1
11-
// newCounter(); // 2
15+
// Return a function that when invoked increments and returns a counter variable.f
16+
let count = 1;
17+
return function() {
18+
return count++;
19+
};
20+
}
21+
22+
23+
const newCounter = counter();
24+
console.log(newCounter()); // 1
25+
console.log(newCounter())// 2
1226

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

1529
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
16-
const counterFactory = () => {
30+
const counterFactory = (x) => {
1731
// Return an object that has two methods called `increment` and `decrement`.
1832
// `increment` should increment a counter variable in closure scope and return it.
1933
// `decrement` should decrement the counter variable and return it.
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax
2+
let myFunction = () => {};
23

3-
// let myFunction = function () {};
4+
let anotherFunction = param => param;
45

5-
// let anotherFunction = function (param) {
6-
// return param;
7-
// };
6+
let add = (param1, param2) => param1 + param2;
87

9-
// let add = function (param1, param2) {
10-
// return param1 + param2;
11-
// };
12-
// add(1,2);
8+
console.log(add(1,2));
139

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

19-
// exampleArray = [1,2,3,4];
20-
// const triple = exampleArray.map(function (num) {
21-
// return num * 3;
22-
// });
23-
// console.log(triple);
12+
console.log(subtract(1,2));
13+
14+
exampleArray = [1,2,3,4];
15+
const triple = exampleArray.map( num => num * 3);
16+
console.log(triple);

0 commit comments

Comments
 (0)