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
82 changes: 81 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,109 @@ 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.
console.log("----------------------array-methods.js challenge 1------------------------");
let fullNames = [];

runners.forEach(function(runner) {
fullNames.push(`${runner.first_name} ${runner.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.
console.log("----------------------array-methods.js challenge 2------------------------");
let firstNamesAllCaps = [];

runners.forEach(function(runner) {
firstNamesAllCaps.push(runner.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.
console.log("----------------------array-methods.js challenge 3------------------------");
let runnersLargeSizeShirt = [];

runnersLargeSizeShirt = runners.filter(function(runner) {
// if(runner.shirt_size === "M")
// return runner;
return runner.shirt_size === "M";
});

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.
console.log("----------------------array-methods.js challenge 4------------------------");
let ticketPriceTotal = 0;

ticketPriceTotal = runners.reduce(function(accu, curEle) {
return accu + curEle.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
//Print every runner object with a first name that starts with a "m"
console.log("----------------------array-methods.js challenge 5 problem 1------------------------");

console.log(runners.filter(function(runner) {
return runner.first_name[0] === "M";
}));

// Problem 2
//Print the average of all runner donations
console.log("----------------------array-methods.js challenge 5 problem 2------------------------");

console.log(runners.reduce(function(accu, runner) {
return accu + runner.donation;
}, 0) / runners.length);

//Problem 3
//Caeser shuffle: shift the characters of the runner's names n places to the right alphabetically
console.log("----------------------array-methods.js challenge 5 problem 3------------------------");

function isUpperCase(letter) {
return letter === letter.toUpperCase();
}

function shiftLetter(letter, n) {
if(!letter.match(/[a-z|A-Z]/g))
return letter;

n %= 26;

var newPlace = letter.charCodeAt(0) + n;
var max = isUpperCase(letter) ? 90 : 122; // Z, z
var min = isUpperCase(letter) ? 65 : 97; // A, a

if(newPlace > max) // 122 = "z"
return String.fromCharCode(newPlace - max + min - 1); // newPlace - 122 + 97 - 1
else if(newPlace < min)
return String.fromCharCode(max - (min - newPlace) + 1);
else
return String.fromCharCode(newPlace);
}

console.log(runners.map(function(runner) {
var n = Math.ceil(Math.random() * 26) * (Math.random() < 0.5 ? 1 : -1);
var runnerCopy = {};

Object.assign(runnerCopy, runner);

runnerCopy.first_name = n + ": " + runnerCopy.first_name.split("").map(function(letter) {
return shiftLetter(letter, n)
}).join("");

runnerCopy.last_name = runnerCopy.last_name.split("").map(function(letter) {
return shiftLetter(letter, n)
}).join("");

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

console.log("----------------------callback.js challenge 1------------------------");

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

console.log(getLength(items, function(len) {
return(`The length of items is ${len}`);
}));

console.log("----------------------callback.js challenge 2------------------------");

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

console.log(last(items, function(lastItem) {
return(`The last item is ${lastItem}`);
}));

console.log("----------------------callback.js challenge 3------------------------");

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

console.log(sumNums(2, 2, function(sum) {
return(`The sum is ${sum}`);
}));

console.log("----------------------callback.js challenge 4------------------------");

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

console.log(multiplyNums(4, 10, function(product) {
return(`The product is ${product}`);
}));

console.log("----------------------callback.js challenge 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.
return cb(list.indexOf(item) !== -1);
}

/* STRETCH PROBLEM */
function cb(isContained) {
return isContained ? "True" : "False";
}

function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
console.log(contains("Gum", items, cb));
console.log(contains("Potato", items, cb));

// console.log(contains("Gum", items, function(isContained) {
// return isContained ? "True" : "False";
// }));

// console.log(contains("Potato", items, function(isContained) {
// return isContained ? "True" : "False";
// }));

/* STRETCH PROBLEM */

console.log("----------------------callback.js challenge 6------------------------");

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.
var result = [];

array.forEach(function(ele) {
result.indexOf(ele) === -1 && result.push(ele);
});

return cb(result);
}

console.log(removeDuplicates([1, 2, 1, 0, 1, 1, 0, 0, "0", 4], function(arr) {
return arr;
}));
101 changes: 89 additions & 12 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,107 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

console.log("----------------------clojure.js challenge 1------------------------");

const countDown = function() {
var num = 10;

return function() {
num--;
return num;
}
}

let counter = countDown();
for(let i = 0; i < 10; i++) {
console.log(counter());
}

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


// ==== Challenge 2: Implement a "counter maker" function ====
// 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.

console.log("----------------------clojure.js challenge 2------------------------");

const counterMaker = () => {
// 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() {
count++;
return count;
}

return counter;
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
/* Example usage: */const myCounter = counterMaker();
console.log(myCounter()); // 1
console.log(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.

console.log("-------------------Closure.js Challenge 3------------------");
const newCounterMaker = (limit) => {
let count = 0;

function counter() {
if(count < limit)
count++;
else
count = 1;

return count;
}

return counter;
};

const myNewCounter = newCounterMaker(4);
console.log(myNewCounter());
console.log(myNewCounter());
console.log(myNewCounter());
console.log(myNewCounter());
console.log(myNewCounter());


// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
// 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.

console.log("----------------------clojure.js challenge 4------------------------");

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.
var count = 0;

var factory = {
increment: function() {
count++;
return count;
},
decrement: function() {
count--;
return count;
}
};

return factory;
};
var factory = counterFactory();
console.log(factory.decrement());
console.log(factory.decrement());
console.log(factory.decrement());
console.log(factory.increment());
console.log(factory.increment());
console.log(factory.increment());
console.log(factory.increment());