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
56 changes: 55 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,82 @@ const runners = [
// ==== 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 and populate a new array called `fullNames`. This array will contain just strings.
let fullNames = [];

runners.forEach(function(element){

fullNames.push(`${element.first_name} ${element.last_name}`);

});

console.log(fullNames);

// ==== Challenge 2: Use .map() ====
// The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings.
let firstNamesAllCaps = [];

firstNamesAllCaps=runners.map(function(element){


return element.first_name.toUpperCase();

});


console.log(firstNamesAllCaps);

// ==== Challenge 3: Use .filter() ====
// The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects.
let runnersLargeSizeShirt = [];

runnersLargeSizeShirt = runners.filter( size => size.shirt_size === 'L');


console.log(runnersLargeSizeShirt);

// ==== Challenge 4: Use .reduce() ====
// The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable.
let ticketPriceTotal = 0;

ticketPriceTotal = runners.reduce(function( accumulator,item){
return accumulator+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
//sort first names
let sortNames = [];

sortNames=runners.map(function(element){


return element.first_name ;

});

console.log(sortNames.sort());


// Problem 2
// how many peopler from skinix company
let skinixComp = [];

skinixComp = runners.filter( element => element.company_name === 'Skinix');


console.log(skinixComp.length);

// Problem 3
//create an array with emails
let emails = [];

runners.forEach(function(element){

emails.push(`${element.email} `);

});

// Problem 3
console.log(emails);
55 changes: 54 additions & 1 deletion assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,85 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
console.log(test2); // "this Pencil is worth a million dollars!"
*/


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

return cb(arr.length);
}

getLength(items, (nwArray) => { console.log(nwArray); });

//********************************************************************************** 2 */

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

}

last(items, (lastItem) => { console.log(lastItem); });

//********************************************************************************** 3 */

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x+ y);
}
sumNums ( 12,34, (sumN) => { console.log(sumN ); });

//********************************************************************************** 4 */

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

multiplyNums (10,12, function(getMultply){
console.log(getMultply);
} );

//********************************************************************************** 5 */

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.
if(list.includes(item)){
return cb(true);
} else {
return cb(false);
}
}

contains ( "Notebook",items , function(checkIt){
console.log(checkIt);
});


//********************************************************************************** stretch */

/* STRETCH PROBLEM */

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.

let fnlArry = [];

array.forEach(function(item){
if(fnlArry.includes(item) === false){
fnlArry.push(item);
}
});


return cb(fnlArry);
}

removeDuplicates(items, function(finalArry){

console.log(finalArry);


});
25 changes: 25 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
// Keep it simple! Remember a closure is just a function
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.
let firstName='Sam';
let lastName='Smith';



function getFullName(){
return (`${firstName} ${lastName}`);
}

console.log(getFullName());

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


Expand All @@ -21,6 +30,22 @@ const counterMaker = () => {
// myCounter(); // 1
// myCounter(); // 2

let count = 0 ;


const myCounter = counterMaker(

function Counter (){

count++;
console.log(count);
return count;

}
);

myCounter();

// ==== Challenge 3: Make `counterMaker` more sophisticated ====
// It should have a `limit` parameter. Any counters we make with `counterMaker`
// will refuse to go over the limit, and start back at 1.
Expand Down