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
26 changes: 22 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,41 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":49,"first_name":"Bel","last_name":"Alway","email":"balway1c@ow.ly","shirt_size":"S","company_name":"Voolia","donation":107},
{"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}];

const newArr = ["stable","barn","table","couch","other thing"];
// ==== 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( function(element) {
return fullName.push(element.first_name + " " + element.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 allCaps = runners.map(function (element) {
return {"id": element.id, "first_name": element.first_name.toUpperCase(), "last_name": element.last_name, "email" : element.email, "shirt_size": element.shirt_size, "company_name": element.company_name, "donation": element.donation}
} );
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( function (element) {
if (element.shirt_size === "L") {
return true;
}
});
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( function (acc, element) {
console.log(acc);
return element.donation += acc


},0);
console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
Expand All @@ -80,4 +97,5 @@ console.log(ticketPriceTotal);

// Problem 2

// Problem 3
// Problem 3

30 changes: 25 additions & 5 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,50 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];


function getLength(arr, cb) {
return cb(arr.getLength)
// getLength passes the length of the array into the callback.
}

function last(arr, cb) {
return cb(arr.length-1)
// last passes the last item of the array into the callback.
}

function sumNums(x, y, cb) {
return cb(x + y)
// sumNums adds two numbers (x, y) and passes the result to the callback.
}

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

function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.
function contains(item, list, cb) {
if (list.indexOf(item) > 0) {
return cb(true)
} else {
return cb(false)
}
// check if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
}
// console.log(contains("seth", r2, uppercase);

/* STRETCH PROBLEM */

/* STRETCH PROBLEM - someone was trying to explain it to me*/
// const numm = [1,2,1,3,1,2]
function removeDuplicates(array, cb) {
// const myArr = array;
// const secondArr = [];
// // console.log(secondArr);
// for (let i = 0; i < myArr.length; i++) {
// console.log(`index:${i} - myArr number :${myArr[i]}`);
// console.log(`secondArr content: ${secondArr}`);
// if (secondArr.indexOf(myArr[i]) === -1)
// secondArr.push(myArr[i])
// }
// return secondArr;
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
}
console.log(removeDuplicates(numm));
27 changes: 27 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

let name = "justine";

function myFunction () {
let lastName = "Gennaro";
return name += " " + lastName;
}
console.log(name);
console.log(myFunction());



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


// ==== Challenge 2: Create a counter function ====
let count = 0;

function counter () {

}
const counter = function () {}
const counter = () => {
let num = 1;
return count += num;
// Return a function that when invoked increments and returns a counter variable.
};
console.log(count);
counter();
console.log(count);
counter();
console.log(count);
counter();
counter();
counter();
console.log(count);
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
Expand Down