Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,51 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
// ==== Challenge 1: Use .forEach() ====
// 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.
let fullName = [];
runners.forEach(runner => {
fullName.push(runner.first_name + " " + runner.last_name);
})
console.log(fullName);

// ==== Challenge 2: Use .map() ====
// 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
let allCaps = [];
let firstName = runners.map((runners) => runners.first_name);
for (let i = 0; i < firstName.length; i++) {
allCaps.push(firstName[i].toUpperCase(firstName[i]));
};
console.log(allCaps);

// ==== Challenge 3: Use .filter() ====
// 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
let largeShirts = [];

let largeShirts = runners.filter(item => item.shirt_size === "L");

console.log(largeShirts);

// ==== Challenge 4: Use .reduce() ====
// 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
let ticketPriceTotal = [];
let ticketPriceTotal = runners.reduce((total,item)=> total + item.donation, 0);
console.log(ticketPriceTotal);

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

// Problem 1

// change all info to caps
let AllCap = runners.map( item => [item.first_name.toUpperCase(), item.last_name.toUpperCase(), item.email.toUpperCase()]);
console.log(AllCap);
// Problem 2
// Change everyones name to by bro
runners.forEach( item => item.first_name = "My Bro " + item.first_name);
console.log(runners);

// Problem 3
// Problem 3
// small shirt poeple become smalls
let smallRunners = [];
smallRunners = runners.filter((element)=>{
return element.shirt_size == "S";
});
smallRunners = smallRunners.map((element)=>{
return (`${element.first_name} ${element.last_name = "Smalls"} `);
});
console.log(smallRunners);
27 changes: 20 additions & 7 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.
// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a
// problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.

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

Expand Down Expand Up @@ -27,29 +28,41 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
console.log(arr.length) ;
}
getLength(items);

function last(arr, cb) {
// last passes the last item of the array into the callback.
console.log(arr[3])
}
last(items)

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
console.log(x + y)
}
sumNums(1, 2);

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
console.log(x * y)
}
multiplyNums(2, 8)

function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
for(let i = 0; i < list.length; i++){
if(list[i] === item){
return cb(true)
}
}
return cb(false)
}

/* STRETCH PROBLEM */
contains('Pencil', items, function(check){
console.log(check)
})


function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
}
32 changes: 24 additions & 8 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,31 @@

// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let total = 0;
function count() {
console.log(++total);
}

return count;
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2

let newCounter = counter();
newCounter();
newCounter();
newCounter();

// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
};
let total = 0
return {
increment: () => {
return ++total;
},
decrement: () => {
return --total;
}
}
}
let counting = counterFactory();
console.log(counting.increment());
console.log(counting.decrement());
2 changes: 1 addition & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
<script src="stretch-function-conversion.js"></script>
<!-- <script src="stretch-function-conversion.js"></script> -->
</head>

<body>
Expand Down