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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5507
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

Dominique
# JavaScript - II

With some basic JavaScript principles in hand, we can now expand our skills out even further by exploring callback functions, array methods, and closure. Finish each task in order as the concepts build on one another.
Expand Down
40 changes: 31 additions & 9 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,50 @@ 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(items) {
fullNames.push(`${items.first_name} ${items.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 = [];
let firstNamesAllCaps = runners.map(function(item) {
return item.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 = [];
let runnersLargeSizeShirt = runners.filter(function(item){
return item.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;
console.log(ticketPriceTotal);
let ticketPriceTotal = runners.reduce(function(acc, item) {
return acc + item.donation;
})
console.log(runners.reduce(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

// Problem 2

// Problem 3
// Problem 1 - filter donations over 100
let donationFilter = runners.filter(function(item) {
return item.donation > 100;
})
console.log(donationFilter);
// Problem 2 - create a mailing list reduce to create , separated string
let emailList = runners.reduce(function(acc, item) {
return acc + "," + item.email;
})
console.log(emailList);

// Problem 3 - separate out companies and donations
let sortCompany = [];
runners.forEach(function(items) {
sortCompany.push(`${items.company_name} ${items.donation}`);
})
console.log(sortCompany);

41 changes: 38 additions & 3 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,64 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

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


function last(arr, cb) {
// last passes the last item of the array into the callback.
}
return cb(arr.pop());
};
last(items, (lastItem) => {
console.log(lastItem);
})


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


function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x, y);
}
const multiply = function(x,y){
return x * y;
}
console.log(sumNums(5,2, multiply));



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.
return cb(item,list);
}

const checkItem = (item, list) => {
return list.includes(item);
}
console.log(contains(items[0],items, checkItem));
/* 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.
return cb(array);
}

const newArray = function(array) {
return array.filter(function(item, index) {
return array.indexOf(item) >= index;
});
};
console.log(newArray(items));
26 changes: 24 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,37 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.


function school(schoolName) {
const module1= 'UI'
const module2='JavaScript'
console.log(`${schoolName} is a school that offers ${module1} and ${module2} as part of their first learning topics`);
function classRoom(className) {
const cohort= 'Web23'
function student(studentName){
console.log(`${studentName} is part of the ${className} ${cohort} cohort at ${schoolName} where she has completed ${module1} and ${module2} modules in her first 3 weeks of school`)
}//closes student
student('Dominique');
}//closes class
classRoom('Web Development')
}//closes school
school('Lambda')
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */


// ==== Challenge 2: Implement a "counter maker" function ====
const counterMaker = () => {
const counterMaker = (limit) => {
// IMPLEMENTATION OF counterMaker:
// 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`!
// 2- Declare a function `counter`. It should increment and return `count`.
// NOTE: This `counter` function, being nested inside `counterMaker`,
// "closes over" the `count` variable. It can "see" it in the parent scope!
// 3- Return the `counter` function.
let count = 0

function counter(){
return increment(count);
}
return counter;
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
Expand All @@ -30,4 +49,7 @@ 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.
return CoubtObj()
};