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
77 changes: 76 additions & 1 deletion src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,111 @@ 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) => {
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
// Return the new array.

// declare result array object to be returned.
const result = [];
// loop over the elements
for (let i = 0; i < elements.length; i++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're good to reuse your .each() method here. Maybe you could give it a try.

// invoke cb function on elements
result.push(cb(elements[i]));
// push result to result array
}
// return result array
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.

// cb(memo, elements[0]);
let result = memo;
// const initialValue = memo;

if (elements.length > 1) {
// Loop over the array
for (let i = 0; i < elements.length; i++) {
// call the cb function on current element in elements.
result = cb(result, elements[i]);
// call shift on elements
// save returned value to memo.
// memo = result;
}
}
return result;
};

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.

// Loop through elements array.
for (let i = 0; i < elements.length; i++) {
// Check truth statement of the callback.
// Return the value when the callback is true.
if (cb(elements[i])) {
return elements[i];
}
}
};

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
// Create a result array that stores all the values that will be returned.

const result = [];
// Loop Through the elements array.
for (let i = 0; i < elements.length; i++) {
// Push elements that pass the test to the result array.
if (cb(elements[i])) {
result.push(elements[i]);
}
}
// Return the result array.
return result;
};

/* 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];
};
let result = [];

function getValues(element, res = []) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice default in the parens.

// loop over all elements in array
for (let i = 0; i < element.length; i++) {
if (typeof element[i] === 'object') {
res = getValues(element[i], result);
} else {
res.push(element[i]);
}
}
return res;
}

for (let i = 0; i < elements.length; i++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what's going on with two different loops here. There may be a better way to solve this. Please refer to the video posted about flatten and reduce, it's up in Week 1 on your piazza account.

if (typeof elements[i] === 'object') {
result = getValues(elements[i], result);
} else {
result.push(elements[i]);
}
}

// result = elements.map(getValues);
return result;
};
/* eslint-enable no-unused-vars, max-len */

module.exports = {
Expand Down
32 changes: 32 additions & 0 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@
// `meow` that should return the string `<name> meowed!` where `<name>` is the `name`
// property set on the Cat instance.

class User {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks really good. It seems like you have a good grasp on these concepts here.

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

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

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

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

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

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

module.exports = {
User,
Expand Down
41 changes: 41 additions & 0 deletions src/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,45 @@ 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 counter1 = 0;

return {
increment: () => {
counter1++;
return counter1;
},
decrement: () => {
counter1--;
return counter1;
}
};
};

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 (...val) => {
count++;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this space right here.

if (count < n) {
return cb(...val);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this space here.

return null;
};
};

/* Extra Credit */
Expand All @@ -26,6 +54,19 @@ 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 arg = [];
const result = [];
return (unknown) => {
if (arg.indexOf(unknown) !== -1) {
return result[arg.indexOf(unknown)];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. I really like indexOf. And I like that you used an array to cache your function call. I would rename this array to something more descriptive other than 'arg' just to make sure if someone was to re-read your code, they'd be able to understand exactly what 'arg' is.

}
const current = cb(unknown);

arg.push(unknown);
result.push(current);

return current;
};
};

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

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

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

var username = 'JavaScriptForever';
var password = 'password';
sayHi(){
return `${this.username} says hello!`;
}
}

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++) {
sum += arguments[i];
function addArgs() {
let sum = 0;
for(let arument of arguments){
sum+=argument;
}
return sum;
};
}

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

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

Expand Down
62 changes: 61 additions & 1 deletion src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,94 @@ 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
return Object.keys(obj);
};

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

const objKeys = Object.keys(obj);
const result = [];

objKeys.forEach((k) => {
return result.push(obj[k]);
});

// for (const key in obj) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just deleted code that you've commented out. It makes for a cleaner submission.

// if (Object.prototype.hasOwnProperty.call(obj, key)) {
// result.push(obj[key]);
// }
// }
return result;
};

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

objKeys.forEach((k) => {
returnObj[k] = cb(obj[k]);
});

return returnObj;
};

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

const objKeys = Object.keys(obj);
const result = [];

objKeys.forEach((k) => {
result.push([k, obj[k]]);
});

return result;
};

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 objKeys = Object.keys(obj);
const returnObj = {};

objKeys.forEach((k) => {
returnObj[obj[k]] = k;
});

return returnObj;
};

const defaults = (obj, defaultProps) => {
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// http://underscorejs.org/#defaults
function hasKey(o, ky) {
// const objKeys = Object.keys(o);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this commented out code

// return objKeys.includes(ky) || o[ky] !== undefined;
return o[ky] === undefined;
}

const defaultKeys = Object.keys(defaultProps);

defaultKeys.forEach((k) => {
if (hasKey(obj, k)) {
obj[k] = defaultProps[k];
}
});

// for (const key in objKeys) {
// if (obj[key] === undefined && defaultProps.hasKey(key)) {
// obj[key] = defaultProps[key];
// }
// }
return obj;
};

/* eslint-enable no-unused-vars */
Expand Down
Loading