Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
108 changes: 54 additions & 54 deletions 108 package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 2 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"babel-jest": "^19.0.0",
"eslint": "^3.17.1",
"eslint-config-airbnb-base": "^11.1.3",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-import": "^2.8.0",
"jest": "^19.0.2",
"regenerator-runtime": "^0.10.3"
},
Expand Down
55 changes: 50 additions & 5 deletions 55 src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,86 @@
// These functions only need to work with arrays.
// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc.
// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating
// You can use the functions that you have already written to help solve the other problems
// You can use the functions that you have already written for the other problems

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
for (let i = 0; i < elements.length; i++) {
cb(elements[i], i);
}
};

// each([1, 2, 3]), (item, index) =>{
// console.log(`${item} @ ${index}`)
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 newArray = [];
each(elements, (item, index) => {
newArray.push(cb(item, index));
});
return newArray;
};

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 considerFirst = true;
if (arguments.length < 3) {
memo = elements[0];
considerFirst = false;
}
each(elements, (element, index) => {
if (index > 0 || considerFirst) {
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.
return elements.filter(cb)[0];
};

const filter = (elements, cb) => {
const filter = (elements, test, 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
// define an empty array
// for each item in the collection array
// if the item passed in the function returns true
// push the item into the defined array
// return the defined array
const filtered = [];
each(elements, (item) => {
if (test(item) === true) {
filtered.push(item);
}
});
return filtered;
};

/* Extra Credit */
// Flattens a nested array (the nesting can be to any depth).
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
const flatten = (elements) => {
// Flattens a nested array (the nesting can be to any depth).
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
let ret = [];
for (let i = 0; i < elements.length; i++) {
if (Array.isArray(elements[i])) {
ret = ret.concat(flatten(elements[i]));
} else {
ret.push(elements[i]);
}
}
return ret;
};
flatten([[[[[0]], [1]], [[[2], [3]]], [[4], [5]]]]);


/* eslint-enable no-unused-vars, max-len */

Expand Down
44 changes: 43 additions & 1 deletion 44 src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,61 @@
// 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.

class User {
constructor(options) {
this.email = options.email;
this.password = options.password;
}

/* eslint-disable no-undef */ // Remove this comment once you write your classes.
comparePasswords(password) {
if (password === this.password) {
return true;
}
return false;
}

}
/* eslint-disable no-undef */


// Create a class called `Animal` and a class called `Cat`.
// `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.

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

class Animal {
constructor(options) {
this.name = options.name;
this.age = options.age;
}
growOlder() {
return this.age += 1;
}
}
class Cat extends Animal {
constructor(options) {
super(options);
this.name = options.name;
}
meow() {
console.log(`${this.name} meowed!`);
}
}

const animal = new Animal({ age: 1 });

const cat = new Cat({
age: 1,
name: 'El Gato'
});
console.log(cat.growOlder());
cat.meow();
console.log(cat.growOlder());

module.exports = {
User,
Expand Down
34 changes: 33 additions & 1 deletion 34 src/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,59 @@ const counter = () => {
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
let count = 0;
return () => {
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 count = 0;
return {
increment: () => { return ++count; },
decrement: () => { return --count; },
};
};

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 (count === n) return null;
count += 1;
return cb(...args);
};
};

/* Extra Credit */
const cacheFunction = (cb) => {
// Should return a funciton that invokes `cb`.
// Should return a function 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 cache = {};
/* return (args) => {
const cacheKey = args.toString();
if (!(cacheKey in cache)) {
cache[cacheKey] = cb(args);
}
return cache[cacheKey];
};
}; */
return (input) => {
// if input is already cached, don't do it
if (Object.prototype.hasOwnProperty.call(cache, input)) return cache[input];
// const something = Object.prototype.hasOwnProperty.call( , );
// if (something) return cache[input];
cache[input] = cb(input);
return cache[input];
};
};

/* eslint-enable no-unused-vars */
Expand Down
26 changes: 13 additions & 13 deletions 26 src/es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,50 @@
//----------------
// const, =>, default parameters, arrow functions default return statements using ()

var food = 'pineapple';
const food = 'pineapple';

var isMyFavoriteFood = function(food) {
let isMyFavoriteFood = function(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);
const isThisMyFavorite = isMyFavoriteFood(food);

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

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

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

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

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

var addArgs = function () {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
const addArgs = function () {
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);
const argsToCb = function (cb) {
let args = Array.prototype.slice.call(arguments);
return cb.apply(null, args.splice(1));
};

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

/* eslint-enable */
48 changes: 43 additions & 5 deletions 48 src/objects.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,78 @@
// Complete the following underscore functions.
// Reference http://underscorejs.org/ for examples.

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
for (let i = 0; i < elements.length; i++) {
cb(elements[i], i);
}
};

const keys = (obj) => {
// Retrieve all the names of the object's properties.
// Return the keys as strings in an array.
// Based on http://underscorejs.org/#keys
const newObj = Object.keys(obj);
return newObj;
// Ryan going over these in today video Sept 11 copy then delete this comment
};

const values = (obj) => {
// Return all of the values of the object's own properties.
// Ignore functions
// http://underscorejs.org/#values
const newObj = Object.values(obj);
return Object.values(newObj);
};

const mapObject = (obj, cb) => {
// Like map for arrays, but for objects. Transform the value of each property in turn.
// Like map for arrays, but for objects. Transform the value of
// each property in turn.
// http://underscorejs.org/#mapObject
const arrayOfKeys = Object.keys(obj);
each(arrayOfKeys, (key) => {
obj[key] = cb(obj[key]);
});
return obj;
};

const pairs = (obj) => {
// Convert an object into a list of [key, value] pairs.
return Object.entries(obj);
// http://underscorejs.org/#pairs
};

const invert = (obj) => {
// Returns a copy of the object where the keys have become the values and the values the keys.
// Assume that all of the object's values will be unique and string serializable.
// http://underscorejs.org/#invert
// return obj;
const result = {};
const newKeys = Object.keys(obj);
for (let i = 0, length = newKeys.length; i < length; i++) {
if (result[obj[newKeys[i]]] instanceof Array) {
result[obj[newKeys[i]]].push(newKeys[i]);
} else if (result[obj[newKeys[i]]]) {
const temp = result[obj[newKeys[i]]];
result[obj[newKeys[i]]] = [temp, newKeys[i]];
} else {
result[obj[newKeys[i]]] = newKeys[i];
}
}
return result;
};

const defaults = (obj, defaultProps) => {
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// http://underscorejs.org/#defaults
// Fill in undefined properties that match properties on the
// `defaultProps` parameter object.
// http://underscorejs.org/#defaults
Object.keys(defaultProps).forEach((key) => {
obj[key] = obj[key] || defaultProps[key];
});
return obj;
};

/* eslint-enable no-unused-vars */

module.exports = {
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.