Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a7a05c8
finished tests; working on implementing functions
MiHHuynh Aug 1, 2017
f461d1a
finished cceptNumbersOnly function
MiHHuynh Aug 1, 2017
2068677
finished mergeArrays function
MiHHuynh Aug 1, 2017
1828583
finished mergeObjects function. all tests passed
MiHHuynh Aug 1, 2017
a458e2d
finished productofarray, collectstrings, contains
MiHHuynh Aug 2, 2017
39ef109
finished search in recursion exercises
MiHHuynh Aug 2, 2017
24b3ca6
fixed typo in recursion tests
MiHHuynh Aug 2, 2017
f621f0a
edited sort function to compare numbers properly
MiHHuynh Aug 2, 2017
f42f98e
working on binary search
MiHHuynh Aug 2, 2017
a772b65
lodash exercises: drop, fromPairs
MiHHuynh Aug 2, 2017
ca2195c
head, take, takeRight, union, zipObject
MiHHuynh Aug 2, 2017
0d0e826
includes, sample, cloneDeep
MiHHuynh Aug 3, 2017
ef62251
some more functions for lodash exercise
MiHHuynh Aug 3, 2017
2beb2c8
added pickBy, omitBy
MiHHuynh Aug 3, 2017
fc8cca5
lodash functions, working on flatten
MiHHuynh Aug 3, 2017
e8ea7cd
initial commit for new pull request
MiHHuynh Aug 3, 2017
f986863
finished canvas shapes game
MiHHuynh Aug 3, 2017
608e512
removed repeated code
MiHHuynh Aug 3, 2017
1c272a4
removed some unnecessary elses
MiHHuynh Aug 3, 2017
8198184
removed if statement in expand
MiHHuynh Aug 3, 2017
d0f6d3e
implemented replaceWith using a map instead of forEach
MiHHuynh Aug 3, 2017
203b72d
reindented everything with spaces instead of tabs
MiHHuynh Aug 4, 2017
7dc77a3
finished ES2015 exercises
MiHHuynh Aug 4, 2017
4cdb916
padEnd, flatten
MiHHuynh Aug 5, 2017
069f8f6
flatten and flattenDeep
MiHHuynh Aug 5, 2017
c1c102d
zip
MiHHuynh Aug 5, 2017
97f33d1
flattenDeep, unzip
MiHHuynh Aug 6, 2017
d3084ba
finished hacker news clone
MiHHuynh Aug 8, 2017
19b05ab
finished lodash exercises and recursion stringifyNumbers
MiHHuynh Aug 8, 2017
c39642a
finished call apply bind exercises - ready for review
MiHHuynh Aug 10, 2017
3284548
finished parts 1 and 2 for prototypes exercises - ready for review
MiHHuynh Aug 10, 2017
a4e9826
incomplete hack or snooze. Need to work on implementing signup/login …
MiHHuynh Aug 14, 2017
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
113 changes: 86 additions & 27 deletions lodash_exercise/lodash.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,98 @@
function drop(){

function drop(arr, num=1){
if (num === 1) {
return arr.slice(1);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't need the else here since you're returning in both cases.

Copy link
Author

Choose a reason for hiding this comment

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

Removed.

return arr.slice(num);
}
}

function fromPairs(){

function fromPairs(arr){
// assuming arr is an array of arrays containing key value pairs
// e.g. [[key, value],[key, value],[key, value]]
if (arr.length === 0) {
return {};
} else {
var result = {};
for (var i = 0; i < arr.length; i++) {
result[arr[i][0]] = arr[i][1];
}
return result;
}
}

function head(){

function head(arr){
if (arr.length === 0) {
return undefined;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above, don't need the else here.

Copy link
Author

Choose a reason for hiding this comment

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

Removed.

return arr[0];
}
}

function take(){

function take(arr, num=1){
return arr.slice(0, num);
}

function takeRight(){

function takeRight(arr, num=1){
if (num === 0) {
return []
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

You guessed it! Don't need the else here as well.

Copy link
Author

Choose a reason for hiding this comment

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

Removed.

return arr.slice(num*-1);
}
}

function union(){

}

function zipObject(){

}

function includes(){

}

function sample(){

}

function cloneDeep(){

// assuming arguments will be an "array" of arrays
var result = [];
for (var i = 0; i < arguments.length; i++) {
for (var j = 0; j < arguments[i].length; j++) {
if (result.indexOf(arguments[i][j]) === -1) {
result.push(arguments[i][j]);
}
}
}
return result;
}

function zipObject(keysArr, valuesArr){
var result = {};
for (var i = 0; i < keysArr.length; i++) {
result[keysArr[i]] = valuesArr[i];
}
return result;
}

function includes(collection, val, fromIndex=0){
// collection can be an array, string, or object
if (typeof collection === "object" && Array.isArray(collection) !== true) {
for (var key in collection) {
if (collection[key] === val) {
return true;
}
}
return false;
}
return (collection.slice(fromIndex).indexOf(val) !== -1) ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! You don't need () around the expression here

Copy link
Author

Choose a reason for hiding this comment

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

Removed.

}

function sample(arr){
return arr[Math.floor(Math.random()*arr.length-1)];
}

function cloneDeep(collection){
var result;
if (Array.isArray(collection)) {
result = [];
collection.forEach(function(val){
// we want to make all the references different
result.concat(cloneDeep(val));
});
} else {
result = {};
for (var key in collection) {
result[key] = collection[key];
}
}
return result;
}

function sumBy(){
Expand Down Expand Up @@ -82,6 +139,8 @@ function flatten(){

}

// BONUS

function zip(){

}
Expand Down
63 changes: 63 additions & 0 deletions recursion_exercise/recursion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function productOfArray(arr) {
if (arr.length === 1) {
return arr[0];
}
return arr[0] * productOfArray(arr.slice(1));
}

function collectStrings(obj) {
var strings = [];
function collectStringsHelper(obj) {
for (var key in obj) {
if (typeof(obj[key]) === "string") {
strings.push(obj[key]);
} else {
collectStringsHelper(obj[key]);
}
}
}
collectStringsHelper(obj);
return strings;
}

function contains(nestedObj, searchTerm) {
for (var key in nestedObj) {
if (typeof(nestedObj[key]) === "object") {
if (contains(nestedObj[key], searchTerm)) {
return true;
};
} else if (nestedObj[key] === searchTerm) {
return true;
}
}
return false;
}

function search(arr, val, currentIndex=0) {
if (arr[0] === val) {
return currentIndex;
}
// if an empty array is passed in, value is not found
else if (arr.length === 0) {
return -1;
}
// otherwise, increment the index to keep track of it, and then remove the
// value that was already checked, and pass in a shorter array to search()
else {
currentIndex++;
currentIndex = search(arr.slice(1), val, currentIndex);
}
return currentIndex;
}

function binarySearch(arr, val) {
// sort the array
Copy link
Contributor

Choose a reason for hiding this comment

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

Try this one when you have some time - you're getting the hang of recursion!

// find the middle, compare
// remove the middle and everything before or after it
// depending on the comparison
// repeat
}

function stringifyNumbers(obj) {

}
4 changes: 2 additions & 2 deletions recursion_exercise/recursionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ describe("#search", function(){
expect(search([1,2,3,4,5,6,7],6)).to.equal(5)
});
it("should return -1 if the value is not found", function(){
expect(search([1,2,3,4]),0).to.equal(-1)
expect(search([1,2]),11).to.equal(-1)
expect(search([1,2,3,4],0)).to.equal(-1)
expect(search([1,2],11)).to.equal(-1)
});
});

Expand Down
47 changes: 47 additions & 0 deletions testing_exercise/testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
function replaceWith(str, replaceThisChar, replacementChar) {
var arrayOfChars = str.split("");
arrayOfChars.forEach(function(val, index){
Copy link
Contributor

Choose a reason for hiding this comment

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

nice work. can you think of a way to refactor this using a map?

Copy link
Author

Choose a reason for hiding this comment

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

Just refactored it with a map. I think it looks cleaner now. :)

function replaceWith(str, replaceThisChar, replacementChar) {
	return str.split("").map(function(val, index, array){
		if (val === replaceThisChar) return replacementChar;
		return val;
	}).join("");
}

if (val === replaceThisChar) {
arrayOfChars[index] = replacementChar;
}
});
return arrayOfChars.join("");
}

function expand(arr, num) {
if (num === 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need this if statement?

Copy link
Author

Choose a reason for hiding this comment

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

I was thinking that in the case of num === 1, or with no repeats, I would just return to save the work of going into the for loop, but I can see that it's just unnecessary code. I'll remove it.

return arr;
}
var result = arr;
for (var i = 1; i < num; i++) {
result = result.concat(arr);
}
return result;
}

function acceptNumbersOnly() {
for (var i = 0; i < arguments.length; i++) {
if (typeof(arguments[i]) !== "number" || isNaN(arguments[i])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

check out the method Number.isFinite - it's a useful way to avoid the logic here

return false;
}
}
return true;
}

function mergeArrays(arr1, arr2) {
var mergedArr = arr1.concat(arr2);
return mergedArr.sort(function(a, b){
return a - b;
});
}

function mergeObjects(obj1, obj2) {
var result = {};
for (var key in obj1) {
result[key] = obj1[key];
}
for (var key in obj2) {
result[key] = obj2[key];
}
return result;
}
70 changes: 69 additions & 1 deletion testing_exercise/testingSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
var expect = chai.expect;

// WRITE YOUR TESTS HERE!
describe("replaceWith", function(){
it("returns a string", function(){
expect(replaceWith("test", "t", "x")).to.be.a("string");
});
it("removes the designated character", function(){
expect(replaceWith("test", "t", "x")).to.not.have.string("t");
});
it("inserts the designated character", function(){
expect(replaceWith("test", "t", "x")).to.have.string("x");
});
it("replaces specific char with designated char", function() {
expect(replaceWith("awesome", "e", "z")).to.deep.equal("awzsomz");
expect(replaceWith("Foo", "F", "B")).to.deep.equal("Boo");
});
it("is case sensitive", function(){
expect(replaceWith("aBcBbBbBb", "B", "c")).to.deep.equal("acccbcbcb");
});
});

describe("expand", function(){
it("returns an array", function(){
expect(expand([1,2,3],3)).to.be.an("array");
});
it("should return an array that contains the original array a number of times", function(){
expect(expand([1,2,3],3)).to.include.members([1,2,3]);
expect(expand([1,2,3],3)).to.deep.equal([1,2,3,1,2,3,1,2,3]);
});
});

describe("acceptNumbersOnly", function(){
it("returns a boolean", function(){
expect(acceptNumbersOnly(1,2,3,4,5,6,7)).to.equal(true);
expect(acceptNumbersOnly(1,2,3,4,5,6,"hi")).to.equal(false);
});
it("returns true if all items in array are numbers", function(){
expect(acceptNumbersOnly(1,2,3,4,5,6,7)).to.be.true;
expect(acceptNumbersOnly(100,200,300,400)).to.be.true;
});
it("returns false if not all items numbers", function(){
expect(acceptNumbersOnly(1, "foo")).to.be.false;
});
it("returns false if NaN is present", function(){
expect(acceptNumbersOnly(1,2,3,4,5,6,NaN)).to.be.false;
});
});

describe("mergeArrays", function(){
it("returns an array", function(){
expect(mergeArrays([2,1],[3,4])).to.be.an("array");
});
it("returns the array sorted", function(){
expect(mergeArrays([2,1],[3,4])).to.deep.equal([1,2,3,4]);
});
});

describe("mergeObjects", function(){
it("should return an object", function(){
expect(mergeObjects({hello:1, bye:2}, {hello2:3, bye2:4})).to.be.an("object");
});
it("should override value in first passed in obj if both objs have the same key", function(){
expect(mergeObjects({hello:1, bye:2}, {hello:3, bye:4})).to.deep.equal({hello:3, bye:4});
});
it("should return an object with all the keys from both objs", function(){
expect(mergeObjects({hello:1, bye:2}, {hello2:3, bye2:4})).to.have.all.keys("hello", "bye", "hello2", "bye2");
});
it("should not change the key-value pairs", function(){
expect(mergeObjects({hello:1, bye:2}, {hello2:3, bye2:4})).to.deep.equal({hello:1, bye:2, hello2:3, bye2:4});
});
});