Skip to content
This repository was archived by the owner on Aug 31, 2019. It is now read-only.
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
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,87 @@ const each = (elements, cb) => {
// Iterates over a list of elements, yielding each in turn to the `cb` function.
// This only needs to work with arrays.
// based off http://underscorejs.org/#each
if (Array.isArray(elements)) {
for (let i = 0; i < elements.length; i++) {
cb(elements[i], i, elements);
}
} else if (elements === null) {
return elements;
}
};

const map = (elements, cb) => {
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
// Return the new array.
const result = [];

each(elements, (item) => {
result.push(cb(item));
});

return result;
};

const reduce = (elements, cb, memo = elements.shift()) => {
// Combine all elements into a single value going from left to right.
// Elements will be passed one by one into `cb`.
// `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
let memoUndefined = arguments.length < 3;
each(elements, (element, index) => {
if (memoUndefined) {
memoUndefined = false;
memo = element;
} else {
return memo = cb(memo, element, index);
}
});

return memo;
};

const find = (elements, cb) => {
// Look through each value in `elements` and pass each element to `cb`.
// If `cb` returns `true` then return that element.
// Return `undefined` if no elements pass the truth test.
const arr = [];

each(elements, (element, index) => {
if (cb(element, index)) {
arr.push(element);
}
});

if (arr.length === 0) {
return undefined;
}

return arr[0];
};

const filter = (elements, cb) => {
// Similar to `find` but you will return an array of all elements that passed the truth test
// Return an empty array if no elements pass the truth test
const arr = [];
each(elements, (element, index) => {
if (cb(element, index)) {
arr.push(element);
}
});

return arr;
};

/* Extra Credit */
const flatten = (elements) => {
// Flattens a nested array (the nesting can be to any depth).
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
each(elements, (element) => {
if (Array.isArray(element)) {
elements = flatten([].concat([], ...elements));
}
});

return elements;
};

/* eslint-enable no-unused-vars, max-len */
Expand Down
50 changes: 45 additions & 5 deletions src/class.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
// Create a class called User.
// Part 1
// Create a class called User using the ES6 class keyword.
// The constructor of the class should have a parameter called `options`.
// `options` will be an object that will have the properties `email` and `password`.
// Set the `email` and `password` properties on the class.
// Add a method called `comparePasswords`. `comparePasswords` should have a parameter
// for a potential password that will be compared to the `password` property.
// Return true if the potential password matches the `password` property. Otherwise return false.

// code here

/* eslint-disable no-undef */ // Remove this comment once you write your classes.
class User {
constructor(options) {
this.email = options.email;
this.password = options.password;
}

comparePasswords(somePassword) {
return somePassword === this.password;
}
}

// Create a class called `Animal` and a class called `Cat`.
// Part 2
// Create a class called `Animal` and a class called `Cat` using ES6 classes.
// `Cat` should extend the `Animal` class.
// Animal and Cat should both have a parameter called `options` in their constructors.
// Animal should have the property `age` that's set in the constructor and the method
// `growOlder` that returns the age.
// `growOlder` that returns the age after incrementing it.
// Cat should have the property `name` that is set in the constructor and the method
// `meow` that should return the string `<name> meowed!` where `<name>` is the `name`
// property set on the Cat instance.

// code here

/* eslint-disable no-undef */

class Animal {
constructor(options, age) {
this.options = options;
this.age = options.age;
}

growOlder() {
this.age = this.age + 1;
return this.age;
}
}

class Cat extends Animal {
constructor(options, name, age) {
super(options, name, age);
this.options = options;
this.name = options.name;
this.age = options.age;
}

meow() {
return `${this.name} meowed!`;
}
}

module.exports = {
User,
Cat
Cat,
Animal,
};
65 changes: 63 additions & 2 deletions src/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,88 @@ const counter = () => {
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
let count = 0;
return () => {
++count;
return count;
};
};

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 s = 0;
return {
increment: () => {
s += 1;
return s;
},
decrement: () => {
s -= 1;
return s;
},
};
};

const limitFunctionCallCount = (cb, n) => {
// Should return a function that invokes `cb`.
// The returned function should only allow `cb` to be invoked `n` times.
let count = 0;

return (...args) => {
if (n === count) {
return null;
}

count++;
return cb(...args);
};
};

/* Extra Credit */
/* STRETCH PROBLEM */

const cacheFunction = (cb) => {
// Should return a funciton that invokes `cb`.
// A cache (object) should be kept in closure scope.
// The cache should keep track of all arguments have been used to invoke this function.
// If the returned function is invoked with arguments that it has already seen
// then it should return the cached result and not invoke `cb` again.
// `cb` should only ever be invoked once for a given set of arguments.
const storage = {};
const args = [];
const tempCB = cb;
let tempFunc = tempCB.toString().split(' ')[1];
if (tempCB !== undefined) {
const ourFunc = tempCB.toString().split(' ')[1].split('');

for (let i = 0; i <= ourFunc.indexOf('(') + 1; i++) {
ourFunc.splice(ourFunc[i], 1);
}

tempFunc = ourFunc;
tempFunc.shift();
tempFunc.pop();

console.log('OUR NEW FUNC SHOUDL BE: ', tempFunc);
}

console.log(tempFunc[0]);

if (storage[tempFunc[0]] === undefined) {
storage[tempFunc[0]] = cb();
}

console.log('RES IS: ', cb(tempFunc[0]));

return function () {
const arg = tempFunc[0];
if (!storage[arg]) {
storage[arg] = cb(arg);
}

return storage[arg];
};
};

/* eslint-enable no-unused-vars */
Expand All @@ -34,5 +95,5 @@ module.exports = {
counter,
counterFactory,
cacheFunction,
limitFunctionCallCount
limitFunctionCallCount,
};
39 changes: 18 additions & 21 deletions src/es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,47 @@
//----------------
// const, =>, default parameters, arrow functions default return statements using ()

var food = 'pineapple';
let food = `pineapple`;

var isMyFavoriteFood = function(food) {
food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey
return food === 'thousand-year-old egg';
const isMyFavoriteFood = (food) => {
food = food || `thousand-year-old egg`; //This sets a default value if `food` is falsey
return food === `thousand-year-old egg`;
};

var isThisMyFavorite = isMyFavoriteFood(food);
let isThisMyFavorite = isMyFavoriteFood(food);

//----------------
//const, class, template literals, enhanced object literals (foo: foo, -> foo,)

var User = function(options) {
const User = (options) => {
this.username = options.username;
this.password = options.password;
this.sayHi = function() {
return this.username + ' says hello!';
this.sayHi = () => {
return `${this.username} says hello!`;
};
}

var username = 'JavaScriptForever';
var password = 'password';
let username = `JavaScriptForever`;
let password = `password`;

var me = new User({
username: username,
password: password,
});
let me = new User({ username, password });

// ----------------
// let, const, =>, ... (spread operator)

var addArgs = function () {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
const addArgs = () => {
let sum = 0;
for (let i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
};

var argsToCb = function (cb) {
var args = Array.prototype.slice.call(arguments);
return cb.apply(null, args.splice(1));
const argsToCb = (cb) => {
const args = Array.prototype.slice.call(arguments);
return cb(...args.splice(1));
};

var result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15
let result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15

/* eslint-enable */
Loading