Skip to content

initial commit #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
32 changes: 29 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,52 @@ 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 = [];
console.log(fullNames);
let fullNames = [];

fullNames = runners.forEach(runners => {
console.log(runners.first_name,runners.last_name);
});

// for(let i = 0; i < runners.length; i++){
// let mappedObj = {};
// mappedObj.first_name = runners[i].first_name;
// mappedObj.last_name = runners[i].last_name;
// fullNames.push(mappedObj);
// mappedObj = {};
// }

// ==== 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((runners) => {
return{
'first': runners.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((runners) => {
return runners.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((total,runners) =>{
return total + runners.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.
// Now that you have used .foEach(), .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

Expand Down
24 changes: 24 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,48 @@ 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, function(length){
console.log(length);
});

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

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

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

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(1);
}else{
return cb(0);
}
}
contains('Gum', items, function(answer){
console.log(answer);
})

/* STRETCH PROBLEM */

Expand Down
36 changes: 34 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
// 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 numbers = [23, 24, 45, 46];


function question(answer){
//What number is you favorite number?
const myQuestion = 'What number is your favorite number?';

return myQuestion + " " + answer;
//return myQuestion + numbers[answer] //option for embedded function.
}
//console.log(question(3));
function answer(i){
return numbers[i]
}
console.log(question(answer(2)));
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */


Expand All @@ -16,7 +28,27 @@ const counterMaker = () => {
// 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 count++; //increasing count
}
return counter();
};

const myCounter = counterMaker();

console.log(myCounter); // 1

console.log(myCounter); // 2

console.log(myCounter); // 3

console.log(myCounter); // 4
console.log(myCounter); // 5

console.log(myCounter); // 1

//console.log(counterMaker);
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
Expand Down