Skip to content
This repository was archived by the owner on Aug 31, 2019. It is now read-only.
Closed
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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "LambdaSchool's JavaScript Mini-Bootcamp Homework 1",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/eslint tests/*.js && ./node_modules/.bin/eslint src/*.js && jest",
"test": "eslint tests/*.js && eslint src/*.js && jest",
"test:watch": "npm test -- --watch"
},
"repository": {
Expand All @@ -27,6 +27,8 @@
},
"dependencies": {
"babel-preset-es2015": "^6.24.0",
"eslint-config-airbnb": "^14.1.0"
"eslint-config-airbnb": "^14.1.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.3"
}
}
42 changes: 39 additions & 3 deletions src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,74 @@

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

// const each = (elements, cb) => elements.forEach((el, index) => cb(el, index));
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 map = (elements, cb) => elements.map(el => cb(el));
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 arr = [];
for (let i = 0; i < elements.length; i++) {
arr.push(cb(elements[i], i));
}
return arr;
};

const reduce = (elements, cb, memo) => {
// const reduce = (elements, cb, memo = 0) => elements.reduce((tot, el) => cb(tot, el), memo);
const reduce = (elements, cb, memo = 0) => {
// 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.
// const result = arrayMethods.reduce(arr, (memo, num) => (memo + num), 10);
let sum = memo;

for (let i = 0; i < elements.length; i++) {
sum = cb(sum, elements[i]);
}
return sum;
};

// const find = (elements, cb) => elements.find(el => cb(el));
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.
let val;

for (let i = 0; i < elements.length; i++) {
if (cb(elements[i])) {
val = elements[i];
break;
}
val = undefined;
}
return val;
};

// const filter = (elements, cb) => elements.filter(el => cb(el));
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 passed = [];
for (let i = 0; i < elements.length; i++) {
if (cb(elements[i])) passed.push(elements[i]);
}
return passed;
};

const flatten = (elements) => {
const flatten = elements => elements.reduce(
(arr, el) => arr.concat(Array.isArray(el) ? flatten(el) : el), []);
// Flattens a nested array (the nesting can be to any depth).
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
};


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

Expand Down
31 changes: 31 additions & 0 deletions src/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,42 @@ const counter = () => {
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
let count = 0;

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

const cacheFunction = (cb) => {
Expand All @@ -27,6 +52,12 @@ const cacheFunction = (cb) => {
// 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 (x) => {
if (!hasOwnProperty.call(cache, x)) cache[x] = cb(x);
return cache[x];
};
};

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

var 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';
};

var isThisMyFavorite = isMyFavoriteFood(food);
const food = 'pineapple';
const isMyFavoriteFood = (food = 'thousand-year-old egg') => food === 'thousand-year-old egg';
const isThisMyFavorite = isMyFavoriteFood(food);

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

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

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

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

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

var addArgs = function () {
var sum = 0;
for (var 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 addArgs = (...args) => args.reduce((sum, num) => sum + num);
const argsToCb = (cb, ...args) => cb.apply(null, args);

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 */
34 changes: 28 additions & 6 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,60 @@

/* eslint-disable no-unused-vars */

const keys = (obj) => {
const keys = obj => Object.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 values = (obj) => {
const values = obj => Object.keys(obj).map(key => obj[key]);
// Return all of the values of the object's own properties.
// Ignore functions
// http://underscorejs.org/#values
};

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

for (let i = 0, len = items.length; i < len; i++) {
copy[items[i]] = cb(obj[items[i]]);
}
return copy;
};

const pairs = (obj) => {
const pairs = obj => Object.keys(obj).map(key => [key, obj[key]]);
// Convert an object into a list of [key, value] pairs.
// 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
const copy = {};
const arr = pairs(obj);

for (let i = 0, len = arr.length; i < len; i++) {
copy[arr[i][1]] = arr[i][0];
}
return copy;
};

const defaults = (obj, defaultProps) => {
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// http://underscorejs.org/#defaults
const objKeys = pairs(obj);
const defKeys = pairs(defaultProps);

for (let i = 0, len = defKeys.length; i < len; i++) {
const key = defKeys[i][0];
const val = defKeys[i][1];

if (!obj[key]) obj[key] = val;
}
return obj;
};

/* eslint-enable no-unused-vars */
Expand Down
28 changes: 28 additions & 0 deletions src/recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,44 @@
const nFibonacci = (n) => {
// fibonacci sequence: 1 2 3 5 8 13 ...
// return the nth number in the sequence
if (n <= 1) return 1;
return nFibonacci(n - 1) + nFibonacci(n - 2);
};

const nFactorial = (n) => {
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
// return the factorial of `n`
if (n === 0) return 1;
return n * nFactorial(n - 1);
};

const checkMatchingLeaves = (obj) => {
// return true if every property on `obj` is the same
// otherwise return false
const values = [];

const flatten = (set) => {
const keys = Object.keys(set);

for (let i = 0, len = keys.length; i < len; i++) {
if (typeof set[keys[i]] !== 'object') {
values.push(set[keys[i]]);
} else {
return flatten(set[keys[i]]);
}
}
return null;
};

const checkVals = (vals) => {
for (let i = 0, len = vals.length; i < len; i++) {
if (vals[i] !== vals[0]) return false;
}
return true;
};

flatten(obj);
return checkVals(values);
};

/* eslint-enable no-unused-vars */
Expand Down
9 changes: 9 additions & 0 deletions src/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
class User {
constructor(options) {
// set a username and password property on the user object that is created
this.username = options.username;
this.password = options.password;
}
// create a method on the User class called `checkPassword`
// this method should take in a string and compare it to the object's password property
// return `true` if they match, otherwise return `false`
checkPassword(str) {
return this.password === str;
}
}

/* eslint-enable no-useless-constructor */
Expand All @@ -23,14 +28,18 @@ const checkPassword = function comparePasswords(passwordToCompare) {
// use `this` to access the object's `password` property.
// do not modify this function's parameters
// note that we use the `function` keyword and not `=>`
return this.password === passwordToCompare;
};

/* eslint-enable no-unused-vars */
// invoke `checkPassword` on `me` by explicitly setting the `this` context
// use .call, .apply, and .bind

// .call
checkPassword.call(me, 'correcthorsebatterystaple');

// .apply
checkPassword.apply(me, ['correcthorsebatterystaple']);

// .bind
checkPassword.bind(me)('correcthorsebatterystaple');