Skip to content

JS Day 2 #788

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 10 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
51 changes: 45 additions & 6 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,67 @@ 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(person){
return fullNames.push(`${person.first_name} ${person.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 = [];

const firstNamesAllCaps = runners.map(function(currentValue) {
return currentValue.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 = [];

const runnersLargeSizeShirt = runners.filter(currentValue => currentValue.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;
// let ticketPriceTotal = 0;

const ticketPriceTotal = runners.reduce(function(accumulator, currentValue) {
// console.log(`I am the accumulator - ${accumulator}.`);
// console.log(`I am the currentValue - ${currentValue}.`);

return accumulator + currentValue.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
// Problem 1 - donations over 250$
const bigBallers = runners.filter(currentValue => currentValue.donation >= 250);

console.log(bigBallers);

// Problem 2 - print out array with full name and contact email
let contactCard = [];

runners.forEach(person => contactCard.push(`${person.first_name} ${person.last_name} - ${person.email}`));

console.log(contactCard);


// Problem 3 - company names in all caps

// Problem 2
const companyAllCaps = runners.map(currentValue => currentValue.company_name.toUpperCase());

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


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

getLength(items, function(arrLength) {
console.log(arrLength);
});

// ES6 way
const getLength2 = (arr, cb) => {
cb(arr.length);
};

getLength2(items, length => {
console.log(`This the the length of the array: ${length}.`);
});


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

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

//ES6
const last2 = (arr, cb) => {
cb(arr[arr.length - 1]);
}

last2(items, lastItem => {
console.log(`This is the last item of the array: ${lastItem}.`);
});

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

sumNums(666, 420, function(x,y) {
console.log(x + y);
});

//ES6
const sumNums2 = (x, y, cb) => {
cb(x + y);
}

sumNums2(420, 666, sumOfTwo => {
console.log(`This is the sum of the chosen two numbers: ${sumOfTwo}.`);
});


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

multiplyNums(666, 420, function(x, y) {
console.log(x * y);
});

//ES6
const multiplyNums2 = (x, y, cb) => {
cb(x * y);
}

multiplyNums2(420, 666, multipleOfTwo => {
console.log(`This is the multiple of the chosen two numbers: ${multipleOfTwo}.`);
});


//first way to do it
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(`Yes, this list contains your item - ${item}.`);
} else {
return cb(`No, this list does not contain your item - ${item}.`);
}
}

contains('yo-yo', items, function(check) {
console.log(check);
})

//second way to do it
const contains2 = (arr, str, cb) => {
const inArray = () => {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === str) {
return `This list contains ${str}.`;
}
} return `This list does not contain ${str}.`;
};
cb(inArray());
};
contains2(items, 'yo-yo', check => {
console.log(check);
});

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
const arrayWithDupes = [6, 666, 13, 420, 99, 'cat', 'tiger', 'cheetah', 'leopard', 666, 420, 13, 'tiger', 'snow leopard', 'jaguar', 'panther', 'lucifer', 'satan']

function removeDuplicates(array, callback) {
// 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 callback(array);
}

console.log(removeDuplicates(arrayWithDupes, function(array) {
let noDupesArray = [];
array.forEach(item => {
if (!noDupesArray.includes(item)) {
noDupesArray.push(item);
}
});
return noDupesArray;
}));
86 changes: 86 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,58 @@
// The outer scope can be a parent function, or the top level of the script.


//NESTED
function genre(genreName, genreName2) {
const origin1 = 'Japan';
const origin2 = 'Russia';
console.log(`These are my favorite genres: ${genreName} and ${genreName2}.`);
//debugger;

function author(authorName, authorName2) {
const lifeStatus = 'dead';
const lifeStatus2 = 'alive';
console.log(`${authorName} is ${lifeStatus2}; ${authorName2} is ${lifeStatus}.`);
//debugger;

function book(title, title2) {
const length = 'light reading';
const length2 = 'heavy read';
console.log(`${title} is a ${length2}; ${title2} is some ${length}.`);
//debugger;
} //closes book
book('Wind-Up Bird Chronicle', 'Notes from Underground');
} //closes author
author('Haruki Murakami', 'Fyodor Dostoevsky');
} //closes genre
genre('Surrealism', 'Russian Classics');


//Closure
const sub = (function() {
var counter = 0;
return function () {
counter += 1;
return counter;
}
}) ();

sub();
sub();
sub();

function letsMultiply(x){
return function(y){
return x * y;
};
}

const mult666 = letsMultiply(666);
const mult13 = letsMultiply(13);

console.log(mult666(13));
console.log(mult13(666));


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


Expand All @@ -16,18 +68,52 @@ 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 +=1);
}
counter();
console.log(count);
counter();
console.log(count);
counter();
console.log(count);
};

console.log(counterMaker());

// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2


// ==== 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.



// ==== Challenge 4: 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 count = 0;

function increment(){
return (count += 1);
}
function decrement(){
return (count -= 1);
}

increment();
increment();
increment();
decrement();
console.log(count);
};

console.log(counterFactory());