From a7a05c87cd303c9de88916e47a8e9ad1d740e196 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Mon, 31 Jul 2017 21:21:25 -0700 Subject: [PATCH 01/32] finished tests; working on implementing functions --- testing_exercise/testing.js | 32 +++++++++++++++ testing_exercise/testingSpec.js | 70 ++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index e69de29b..e1388bda 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -0,0 +1,32 @@ +function replaceWith(str, replaceThisChar, replacementChar) { + var arrayOfChars = str.split(""); + arrayOfChars.forEach(function(val, index){ + if (val === replaceThisChar) { + arrayOfChars[index] = replacementChar; + } + }); + return arrayOfChars.join(""); +} + +function expand(arr, num) { + if (num === 1) { + return arr; + } + var result = arr; + for (var i = 1; i < num; i++) { + result = result.concat(arr); + } + return result; +} + +function acceptNumbersOnly() { + +} + +function mergeArrays(arr1, arr2) { + +} + +function mergeObjects(obj1, obj2) { + +} \ No newline at end of file diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index aef56b1d..0d9cbecc 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -1,3 +1,71 @@ var expect = chai.expect; -// WRITE YOUR TESTS HERE! \ No newline at end of file +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}); + }); +}); \ No newline at end of file From f461d1adb0ac3892f2835a7c6329b1f4009e8f3c Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Mon, 31 Jul 2017 21:27:12 -0700 Subject: [PATCH 02/32] finished cceptNumbersOnly function --- testing_exercise/testing.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index e1388bda..cc4b95de 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -20,7 +20,12 @@ function expand(arr, num) { } function acceptNumbersOnly() { - + for (var i = 0; i < arguments.length; i++) { + if (typeof(arguments[i]) !== "number" || isNaN(arguments[i])) { + return false; + } + } + return true; } function mergeArrays(arr1, arr2) { From 206867769368ef7769b7f9a28069d0fb387867e1 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Mon, 31 Jul 2017 21:29:56 -0700 Subject: [PATCH 03/32] finished mergeArrays function --- testing_exercise/testing.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index cc4b95de..a9bfc6d6 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -29,7 +29,8 @@ function acceptNumbersOnly() { } function mergeArrays(arr1, arr2) { - + var mergedArr = arr1.concat(arr2); + return mergedArr.sort(); } function mergeObjects(obj1, obj2) { From 18285839fea8eb31b28a0e686972c1ffc05e3c7f Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Mon, 31 Jul 2017 21:35:43 -0700 Subject: [PATCH 04/32] finished mergeObjects function. all tests passed --- testing_exercise/testing.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index a9bfc6d6..b81f36ee 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -34,5 +34,12 @@ function mergeArrays(arr1, arr2) { } 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; } \ No newline at end of file From a458e2ded97d38520f34c2d3cdeac5358032f19d Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Tue, 1 Aug 2017 17:25:00 -0700 Subject: [PATCH 05/32] finished productofarray, collectstrings, contains --- recursion_exercise/recursion.js | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index e69de29b..d248a89b 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -0,0 +1,51 @@ +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) { + // if (arr.length === 0) return -1; + // if (arr[0] === val) { + // return 0; + // } else { + + // } +} + +function binarySearch(arr, val) { + +} + +function stringifyNumbers(obj) { + +} \ No newline at end of file From 39ef10997ad7622c2c797ce0603a2da53e0fcdf7 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Wed, 2 Aug 2017 11:07:01 -0700 Subject: [PATCH 06/32] finished search in recursion exercises --- recursion_exercise/recursion.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index d248a89b..d11c4d9e 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -33,13 +33,21 @@ function contains(nestedObj, searchTerm) { return false; } -function search(arr, val) { - // if (arr.length === 0) return -1; - // if (arr[0] === val) { - // return 0; - // } else { - - // } +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) { From 24b3ca6dcfc29b88db5984a12d00ff08ab46f4dc Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Wed, 2 Aug 2017 11:09:19 -0700 Subject: [PATCH 07/32] fixed typo in recursion tests --- recursion_exercise/recursionSpec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/recursion_exercise/recursionSpec.js b/recursion_exercise/recursionSpec.js index 5bc841fe..2306ee0a 100644 --- a/recursion_exercise/recursionSpec.js +++ b/recursion_exercise/recursionSpec.js @@ -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) }); }); From f621f0a412a2667b73df32c76b09d099fa76c876 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Wed, 2 Aug 2017 11:45:09 -0700 Subject: [PATCH 08/32] edited sort function to compare numbers properly --- testing_exercise/testing.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index b81f36ee..0d54f8b7 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -30,7 +30,9 @@ function acceptNumbersOnly() { function mergeArrays(arr1, arr2) { var mergedArr = arr1.concat(arr2); - return mergedArr.sort(); + return mergedArr.sort(function(a, b){ + return a - b; + }); } function mergeObjects(obj1, obj2) { From f42f98e8af2691bae7dcbc4a7570f3f1cb39bd65 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Wed, 2 Aug 2017 11:45:54 -0700 Subject: [PATCH 09/32] working on binary search --- recursion_exercise/recursion.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index d11c4d9e..1322f5f4 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -51,7 +51,11 @@ function search(arr, val, currentIndex=0) { } function binarySearch(arr, val) { - + // sort the array + // find the middle, compare + // remove the middle and everything before or after it + // depending on the comparison + // repeat } function stringifyNumbers(obj) { From a772b65696fff0b6725f207cef031ad1801b3daa Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Wed, 2 Aug 2017 14:28:22 -0700 Subject: [PATCH 10/32] lodash exercises: drop, fromPairs --- lodash_exercise/lodash.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 483d734a..d1255da2 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,9 +1,23 @@ -function drop(){ - -} - -function fromPairs(){ - +function drop(arr, num=1){ + if (num === 1) { + return arr.slice(1); + } else { + return arr.slice(num); + } +} + +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(){ From ca2195caeec6b94f8a2978e006326a2489171521 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Wed, 2 Aug 2017 15:27:29 -0700 Subject: [PATCH 11/32] head, take, takeRight, union, zipObject --- lodash_exercise/lodash.js | 41 ++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index d1255da2..ac58e997 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -20,24 +20,45 @@ function fromPairs(arr){ } } -function head(){ - +function head(arr){ + if (arr.length === 0) { + return undefined; + } else { + 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 { + return arr.slice(num*-1); + } } function union(){ - + // 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(){ - +function zipObject(keysArr, valuesArr){ + var result = {}; + for (var i = 0; i < keysArr.length; i++) { + result[keysArr[i]] = valuesArr[i]; + } + return result; } function includes(){ @@ -96,6 +117,8 @@ function flatten(){ } +// BONUS + function zip(){ } From 0d0e8263d30942c1200d61df88cae73c0f3f766e Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Wed, 2 Aug 2017 17:04:58 -0700 Subject: [PATCH 12/32] includes, sample, cloneDeep --- lodash_exercise/lodash.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index ac58e997..acf1446f 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -61,16 +61,38 @@ function zipObject(keysArr, valuesArr){ return result; } -function includes(){ - +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; } -function sample(){ - +function sample(arr){ + return arr[Math.floor(Math.random()*arr.length-1)]; } -function cloneDeep(){ - +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(){ From ef62251933385b0d1e7eba83b0d50fe71d3deba4 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 08:56:47 -0700 Subject: [PATCH 13/32] some more functions for lodash exercise --- lodash_exercise/lodash.js | 77 +++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index acf1446f..c25299e5 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -95,11 +95,35 @@ function cloneDeep(collection){ return result; } -function sumBy(){ - +function sumBy(arrOfObj, iteratee){ + if (typeof iteratee === "string") { + return arrOfObj.reduce(function(acc, obj){ + acc += obj[iteratee]; + return acc; + }, 0); + } else { + var sum = 0; + for (var i = 0; i < arrOfObj.length; i++) { + sum += iteratee(arrOfObj[i]); + } + return sum; + } } -function inRange(){ +function inRange(num, start=0, end){ + // Checks if n is between start and up to, but not including, end. + // If end is not specified, it's set to start with start then set to 0. + // If start is greater than end the params are swapped to support negative ranges. + if (end === undefined) { + end = start; + start = 0; + } + if (start > end) { + var hold = start; + start = end; + end = hold; + } + return (num > start && num < end); } @@ -107,16 +131,28 @@ function has(){ } -function omit(){ - +function omit(obj, arrKeysToOmit){ + var result = {}; + for (var key in obj) { + if (arrKeysToOmit.indexOf(key) === -1) { + result[key] = obj[key]; + } + } + return result; } -function pick(){ - +function pick(obj, arrKeysToPick){ + var result = {}; + for (var key in obj) { + if (arrKeysToPick.indexOf(key) !== -1) { + result[key] = obj[key]; + } + } + return result; } function pickBy(){ - + } function omitBy(){ @@ -127,16 +163,33 @@ function padEnd(){ } -function repeat(){ +function repeat(str, times){ + if (times === 0) return ""; + if (times === 1) return str; + var result = ""; + for (var i = 0; i < times; i++) { + result = result.concat(str); + } + return result; } function upperFirst(str){ - + var result = ""; + return result.concat(str[0].toUpperCase() + str.slice(1)); } -function flatten(){ - +function flatten(arr){ + //NOT WORKING + var result = []; + for (var i = 0; i < arr.length; i++) { + if (Array.isArray(arr[i])) { + flatten(arr[i]); + } else { + result.push(arr[i]); + } + } + return result; } // BONUS From 2beb2c84ddfcc239140bd93bde3608071e1a5335 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 09:05:54 -0700 Subject: [PATCH 14/32] added pickBy, omitBy --- lodash_exercise/lodash.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index c25299e5..9788c07c 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -151,12 +151,24 @@ function pick(obj, arrKeysToPick){ return result; } -function pickBy(){ - +function pickBy(obj, callback){ + var result = {}; + for (var key in obj) { + if (callback(obj[key])) { + result[key] = obj[key]; + } + } + return result; } -function omitBy(){ - +function omitBy(obj, callback){ + var result = {}; + for (var key in obj) { + if (!callback(obj[key])) { + result[key] = obj[key]; + } + } + return result; } function padEnd(){ From fc8cca591256a95a3fc3ff87b9645025ca18a491 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 12:00:40 -0700 Subject: [PATCH 15/32] lodash functions, working on flatten --- lodash_exercise/lodash.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 9788c07c..b02f4383 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -87,12 +87,12 @@ function cloneDeep(collection){ result.concat(cloneDeep(val)); }); } else { - result = {}; - for (var key in collection) { - result[key] = collection[key]; - } - } - return result; + result = {}; + for (var key in collection) { + result[key] = collection[key]; + } +} +return result; } function sumBy(arrOfObj, iteratee){ @@ -171,8 +171,13 @@ function omitBy(obj, callback){ return result; } -function padEnd(){ - +function padEnd(str="", length=0, chars=" "){ + //NOT WORKING + var result = str.slice(); + if (length === 0) { + result result; + } + return result; } function repeat(str, times){ From e8ea7cddef5cda15417a9d24ae13cd35c55eb6f7 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 12:01:18 -0700 Subject: [PATCH 16/32] initial commit for new pull request --- canvas_exercise/shapes_game/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 0de5f18a..958fe7b9 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -1,4 +1,5 @@ window.addEventListener("load", function() { + // adding a comment here to trigger a commit for new pull request function clear(ctx, width, heigt) { } From f986863aad88cf8ba86e3a5191caa1486ff35be4 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 16:02:26 -0700 Subject: [PATCH 17/32] finished canvas shapes game --- canvas_exercise/shapes_game/index.js | 113 +++++++++++++++++++++++-- canvas_exercise/shapes_game/styles.css | 4 +- 2 files changed, 108 insertions(+), 9 deletions(-) diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 958fe7b9..0a25d403 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -1,37 +1,136 @@ window.addEventListener("load", function() { - // adding a comment here to trigger a commit for new pull request - function clear(ctx, width, heigt) { + function clear(ctx, width, height) { + ctx.clearRect(0, 0, width, height); } function drawRandomShape(ctx, width, height) { + var randomShapeNumber = Math.floor(Math.random()*(4)+1); + var x = Math.floor(Math.random()*642); // multiplied by 692 to ensure shape doesn't go off black screen + var y = Math.floor(Math.random()*592); // multiplied by 642 to ensure shape doesn't go off black screen + + // 1 - White Triangle + if (randomShapeNumber === 1) { + expectedKey = "white0"; + ctx.fillStyle = "white"; + ctx.beginPath(); + ctx.moveTo(x, y); // random point on screen + ctx.lineTo(x+100, y+100); // + ctx.lineTo(x, y+100); + ctx.fill(); + ctx.closePath(); + } + + // 2 - Red Square + if (randomShapeNumber === 2) { + expectedKey = "red1"; + ctx.fillStyle = "red"; + ctx.fillRect(x, y, 100, 100); + } + + + // 3 - Red Triangle + if (randomShapeNumber === 3) { + expectedKey = "red0"; + ctx.fillStyle = "red"; + ctx.beginPath(); + ctx.moveTo(x, y); + ctx.lineTo(x+100, y+100); + ctx.lineTo(x, y+100); + ctx.fill(); + ctx.closePath(); + } + + // 4 - White Square + if (randomShapeNumber === 4) { + expectedKey = "white1"; + ctx.fillStyle = "white"; + ctx.fillRect(x, y, 100, 100); + } + } function drawGameStartText(ctx, width, height, score) { + clear(ctx, width, height); // clear the board of any text or shapes + seconds = 30; // restart the seconds + timerSpan.innerText = "30"; // restart the timerSpan + scoreSpan.innerText = "0"; // restart the score + + ctx.fillStyle = "white"; // re-render start text + ctx.font = "2em Times New Roman"; + ctx.fillText("Score: " + score, 120, 300); + + ctx.fillStyle = "white"; // re-render start text + ctx.font = "2.3em Times New Roman"; + ctx.fillText("Press the space bar to start the game.", 120, 350); + + score = 0; } function restartGame(ctx, width, height) { + clear(ctx, width, height); // clear the board of any text or shapes + seconds = 30; // restart the seconds + timerSpan.innerText = "30"; // restart the timerSpan + scoreSpan.innerText = "0"; // restart the score + ctx.fillStyle = "white"; // re-render start text + ctx.font = "2.3em Times New Roman"; + ctx.fillText("Press the space bar to start the game.", 120, 350); } + function runTimer() { + intervalId = setInterval(function(){ + if (seconds === 0) { + clearInterval(intervalId); + drawGameStartText(ctx, width, height, score); + gameOn = !gameOn; + } else { + seconds--; + timerSpan.innerText = seconds; + } + }, 1000); + }; + var canvas = document.getElementById("shapes-game"), height = canvas.scrollHeight, width = canvas.scrollWidth, gameOn = false, expectedKey = undefined, ctx = canvas.getContext('2d'), - // white triangle = up, red square = down, - // red triangle = left, white square = right + // white triangle / white0 = up, red square / red1 = down, + // red triangle / red0 = left, white square / white1 = right expectedKeysMap = {white0: 38, red1: 40, red0: 37, white1: 39}, timerSpan = document.getElementById("time-remaining"), scoreSpan = document.getElementById("score-val"), - seconds = 3, + score = 0, + seconds = 30, // added a 0 to make it 30? intervalId; canvas.width = width; canvas.height = height; - document.addEventListener("keyup", function() { - + document.addEventListener("keyup", function(e) { + // if gameOn is true, check which key is pressed and address accordingly + if (gameOn) { + if (e.keyCode === expectedKeysMap[expectedKey]) { + score++; + scoreSpan.innerText = score; + clear(ctx, width, height); + drawRandomShape(ctx, width, height); + } else { + score--; + scoreSpan.innerText = score; + } + + } else { // if gameOn is false, only start game if space bar is clicked + if (e.keyCode === 32) { + gameOn = !gameOn; + clear(ctx, width, height); + drawRandomShape(ctx, width, height); + runTimer(); + } + } }); + + restartGame(ctx, width, height); }); diff --git a/canvas_exercise/shapes_game/styles.css b/canvas_exercise/shapes_game/styles.css index cbe4fa83..189431e4 100644 --- a/canvas_exercise/shapes_game/styles.css +++ b/canvas_exercise/shapes_game/styles.css @@ -11,8 +11,8 @@ body { padding: 10px; } .canvas-container, #shapes-game { - height: 750px; - width: 800px; + height: 700px; + width: 750px; } .scores { From 608e512174593c57fc4e66cf5506da49f3e0182f Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 16:12:33 -0700 Subject: [PATCH 18/32] removed repeated code --- canvas_exercise/shapes_game/index.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 0a25d403..a7bd2f56 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -56,9 +56,11 @@ window.addEventListener("load", function() { timerSpan.innerText = "30"; // restart the timerSpan scoreSpan.innerText = "0"; // restart the score - ctx.fillStyle = "white"; // re-render start text - ctx.font = "2em Times New Roman"; - ctx.fillText("Score: " + score, 120, 300); + if (alreadyPlayed) { + ctx.fillStyle = "white"; // re-render start text + ctx.font = "2em Times New Roman"; + ctx.fillText("Score: " + score, 120, 300); + } ctx.fillStyle = "white"; // re-render start text ctx.font = "2.3em Times New Roman"; @@ -67,16 +69,6 @@ window.addEventListener("load", function() { score = 0; } - function restartGame(ctx, width, height) { - clear(ctx, width, height); // clear the board of any text or shapes - seconds = 30; // restart the seconds - timerSpan.innerText = "30"; // restart the timerSpan - scoreSpan.innerText = "0"; // restart the score - ctx.fillStyle = "white"; // re-render start text - ctx.font = "2.3em Times New Roman"; - ctx.fillText("Press the space bar to start the game.", 120, 350); - } - function runTimer() { intervalId = setInterval(function(){ if (seconds === 0) { @@ -94,6 +86,7 @@ window.addEventListener("load", function() { height = canvas.scrollHeight, width = canvas.scrollWidth, gameOn = false, + alreadyPlayed = false, expectedKey = undefined, ctx = canvas.getContext('2d'), // white triangle / white0 = up, red square / red1 = down, @@ -124,6 +117,7 @@ window.addEventListener("load", function() { } else { // if gameOn is false, only start game if space bar is clicked if (e.keyCode === 32) { gameOn = !gameOn; + alreadyPlayed = true; clear(ctx, width, height); drawRandomShape(ctx, width, height); runTimer(); @@ -131,6 +125,6 @@ window.addEventListener("load", function() { } }); - restartGame(ctx, width, height); + drawGameStartText(ctx, width, height); }); From 1c272a47172b649fed187ad76cd1bf1149fbf04c Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 16:26:44 -0700 Subject: [PATCH 19/32] removed some unnecessary elses --- lodash_exercise/lodash.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index b02f4383..6b60a366 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,9 +1,8 @@ function drop(arr, num=1){ if (num === 1) { return arr.slice(1); - } else { - return arr.slice(num); } + return arr.slice(num); } function fromPairs(arr){ @@ -23,9 +22,8 @@ function fromPairs(arr){ function head(arr){ if (arr.length === 0) { return undefined; - } else { - return arr[0]; } + return arr[0]; } function take(arr, num=1){ @@ -35,9 +33,8 @@ function take(arr, num=1){ function takeRight(arr, num=1){ if (num === 0) { return [] - } else { - return arr.slice(num*-1); } + return arr.slice(num*-1); } function union(){ @@ -71,7 +68,7 @@ function includes(collection, val, fromIndex=0){ } return false; } - return (collection.slice(fromIndex).indexOf(val) !== -1) ? true : false; + return collection.slice(fromIndex).indexOf(val) !== -1 ? true : false; } function sample(arr){ From 81981847d80805ebcfd556539345aa7b45adc9b1 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 16:36:16 -0700 Subject: [PATCH 20/32] removed if statement in expand --- testing_exercise/testing.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index 0d54f8b7..f7e957d1 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -9,9 +9,6 @@ function replaceWith(str, replaceThisChar, replacementChar) { } function expand(arr, num) { - if (num === 1) { - return arr; - } var result = arr; for (var i = 1; i < num; i++) { result = result.concat(arr); From d0f6d3e4e5fd23f471db885388c4b8b19fb90209 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 16:53:18 -0700 Subject: [PATCH 21/32] implemented replaceWith using a map instead of forEach --- testing_exercise/testing.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index f7e957d1..3a93df66 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -1,11 +1,15 @@ function replaceWith(str, replaceThisChar, replacementChar) { - var arrayOfChars = str.split(""); - arrayOfChars.forEach(function(val, index){ - if (val === replaceThisChar) { - arrayOfChars[index] = replacementChar; - } - }); - return arrayOfChars.join(""); + // var arrayOfChars = str.split(""); + // arrayOfChars.forEach(function(val, index){ + // if (val === replaceThisChar) { + // arrayOfChars[index] = replacementChar; + // } + // }); + // return arrayOfChars.join(""); + return str.split("").map(function(val, index, array){ + if (val === replaceThisChar) return replacementChar; + return val; + }).join(""); } function expand(arr, num) { From 203b72deae4e284b02f7ad516a290c0974dedba3 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 3 Aug 2017 17:01:35 -0700 Subject: [PATCH 22/32] reindented everything with spaces instead of tabs --- lodash_exercise/lodash.js | 283 +++++++++++++++++++------------------- 1 file changed, 141 insertions(+), 142 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 6b60a366..389ede8a 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,127 +1,126 @@ function drop(arr, num=1){ - if (num === 1) { - return arr.slice(1); - } - return arr.slice(num); + if (num === 1) { + return arr.slice(1); + } + return arr.slice(num); } 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; - } +// 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(arr){ - if (arr.length === 0) { - return undefined; - } - return arr[0]; + if (arr.length === 0) { + return undefined; + } + return arr[0]; } function take(arr, num=1){ - return arr.slice(0, num); + return arr.slice(0, num); } function takeRight(arr, num=1){ - if (num === 0) { - return [] - } - return arr.slice(num*-1); + if (num === 0) { + return [] + } + return arr.slice(num*-1); } function union(){ - // 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; +// 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; + 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; +// 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; } function sample(arr){ - return arr[Math.floor(Math.random()*arr.length-1)]; + 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]; + 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; + return result; } function sumBy(arrOfObj, iteratee){ - if (typeof iteratee === "string") { - return arrOfObj.reduce(function(acc, obj){ - acc += obj[iteratee]; - return acc; - }, 0); - } else { - var sum = 0; - for (var i = 0; i < arrOfObj.length; i++) { - sum += iteratee(arrOfObj[i]); - } - return sum; - } + if (typeof iteratee === "string") { + return arrOfObj.reduce(function(acc, obj){ + acc += obj[iteratee]; + return acc; + }, 0); + } else { + var sum = 0; + for (var i = 0; i < arrOfObj.length; i++) { + sum += iteratee(arrOfObj[i]); + } + return sum; + } } function inRange(num, start=0, end){ - // Checks if n is between start and up to, but not including, end. - // If end is not specified, it's set to start with start then set to 0. - // If start is greater than end the params are swapped to support negative ranges. - if (end === undefined) { - end = start; - start = 0; - } - if (start > end) { - var hold = start; - start = end; - end = hold; - } - return (num > start && num < end); - +// Checks if n is between start and up to, but not including, end. +// If end is not specified, it's set to start with start then set to 0. +// If start is greater than end the params are swapped to support negative ranges. + if (end === undefined) { + end = start; + start = 0; + } + if (start > end) { + var hold = start; + start = end; + end = hold; + } + return (num > start && num < end); } function has(){ @@ -129,81 +128,81 @@ function has(){ } function omit(obj, arrKeysToOmit){ - var result = {}; - for (var key in obj) { - if (arrKeysToOmit.indexOf(key) === -1) { - result[key] = obj[key]; - } - } - return result; + var result = {}; + for (var key in obj) { + if (arrKeysToOmit.indexOf(key) === -1) { + result[key] = obj[key]; + } + } + return result; } function pick(obj, arrKeysToPick){ - var result = {}; - for (var key in obj) { - if (arrKeysToPick.indexOf(key) !== -1) { - result[key] = obj[key]; - } - } - return result; + var result = {}; + for (var key in obj) { + if (arrKeysToPick.indexOf(key) !== -1) { + result[key] = obj[key]; + } + } + return result; } function pickBy(obj, callback){ - var result = {}; - for (var key in obj) { - if (callback(obj[key])) { - result[key] = obj[key]; - } - } - return result; + var result = {}; + for (var key in obj) { + if (callback(obj[key])) { + result[key] = obj[key]; + } + } + return result; } function omitBy(obj, callback){ - var result = {}; - for (var key in obj) { - if (!callback(obj[key])) { - result[key] = obj[key]; - } - } - return result; + var result = {}; + for (var key in obj) { + if (!callback(obj[key])) { + result[key] = obj[key]; + } + } + return result; } function padEnd(str="", length=0, chars=" "){ - //NOT WORKING - var result = str.slice(); - if (length === 0) { - result result; - } - return result; + //NOT WORKING + var result = str.slice(); + if (length === 0) { + result result; + } + return result; } function repeat(str, times){ - if (times === 0) return ""; - if (times === 1) return str; + if (times === 0) return ""; + if (times === 1) return str; - var result = ""; - for (var i = 0; i < times; i++) { - result = result.concat(str); - } - return result; + var result = ""; + for (var i = 0; i < times; i++) { + result = result.concat(str); + } + return result; } function upperFirst(str){ - var result = ""; - return result.concat(str[0].toUpperCase() + str.slice(1)); + var result = ""; + return result.concat(str[0].toUpperCase() + str.slice(1)); } function flatten(arr){ - //NOT WORKING - var result = []; - for (var i = 0; i < arr.length; i++) { - if (Array.isArray(arr[i])) { - flatten(arr[i]); - } else { - result.push(arr[i]); - } - } - return result; + //NOT WORKING + var result = []; + for (var i = 0; i < arr.length; i++) { + if (Array.isArray(arr[i])) { + flatten(arr[i]); + } else { + result.push(arr[i]); + } + } + return result; } // BONUS From 7dc77a374c7ac7993a42650799fb8520255689f8 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Fri, 4 Aug 2017 15:53:19 -0700 Subject: [PATCH 23/32] finished ES2015 exercises --- es2015_exercise/readme.md | 67 +++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/es2015_exercise/readme.md b/es2015_exercise/readme.md index 9e482efa..3d02d4b6 100644 --- a/es2015_exercise/readme.md +++ b/es2015_exercise/readme.md @@ -5,8 +5,8 @@ Convert the following es5 code blocks into es2015 code: ```javascript var person = { fullName: "Harry Potter", - sayHi: function(){ - setTimeout(function(){ + sayHi(){ + setTimeout(() => { console.log("Your name is " + this.fullName) }.bind(this),1000) } @@ -15,26 +15,37 @@ var person = { ```javascript var name = "Josie" -console.log("When " + name + " comes home, so good") +console.log(`When ${name} comes home, so good`); ``` ```javascript -var DO_NOT_CHANGE = 42; -DO_NOT_CHANGE = 50; // stop me from doing this! +// var DO_NOT_CHANGE = 42; +// DO_NOT_CHANGE = 50; // stop me from doing this! + +const DO_NOT_CHANGE = 42 ``` ```javascript +// var arr = [1,2] +// var temp = arr[0] +// arr[0] = arr[1] +// arr[1] = temp + var arr = [1,2] -var temp = arr[0] +let temp = arr[0] arr[0] = arr[1] arr[1] = temp ``` ```javascript +// function double(arr){ +// return arr.map(function(val){ +// return val*2 +// }); +// } + function double(arr){ - return arr.map(function(val){ - return val*2 - }); + return arr.map(val => val*2); } ``` @@ -46,30 +57,38 @@ var obj = { } } -var a = obj.numbers.a; -var b = obj.numbers.b; +// var a = obj.numbers.a; +// var b = obj.numbers.b; + +var {a, b} = obj.numbers ``` ```javascript -function add(a,b){ - if(a === 0) a = 0 - else { - a = a || 10 - } - if(b === 0) b = 0 - else { - b = b || 10 - } +// function add(a,b){ +// if(a === 0) a = 0 +// else { +// a = a || 10 +// } +// if(b === 0) b = 0 +// else { +// b = b || 10 +// } +// return a+b +// } + +function add(a=10, b=10){ return a+b } ``` Research the following functions - what do they do? -`Array.from` - +`Array.from` - The Array.from() method creates a new Array instance from an array-like or iterable object. + +`Object.assign` - The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. It makes a shallow copy. + +`Array.includes` - The includes() method determines whether an array includes a certain element, returning true or false as appropriate. -`Object.assign` - +`String.startsWith` - The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate. -`Array.includes` - -`String.startsWith` - From 4cdb9168f3de11700fafd610914ac2cbf41d58b3 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Sat, 5 Aug 2017 15:12:50 -0700 Subject: [PATCH 24/32] padEnd, flatten --- lodash_exercise/index.html | 1 - lodash_exercise/lodash.js | 324 ++++++++++++++++++++----------------- 2 files changed, 180 insertions(+), 145 deletions(-) diff --git a/lodash_exercise/index.html b/lodash_exercise/index.html index 01b829ff..8570e625 100644 --- a/lodash_exercise/index.html +++ b/lodash_exercise/index.html @@ -1,7 +1,6 @@ - Document diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 389ede8a..902eac86 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,208 +1,243 @@ function drop(arr, num=1){ - if (num === 1) { - return arr.slice(1); - } - return arr.slice(num); + if (num === 1) { + return arr.slice(1); + } + return arr.slice(num); } 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; - } + // 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(arr){ - if (arr.length === 0) { - return undefined; - } - return arr[0]; + if (arr.length === 0) { + return undefined; + } + return arr[0]; } function take(arr, num=1){ - return arr.slice(0, num); + return arr.slice(0, num); } function takeRight(arr, num=1){ - if (num === 0) { - return [] - } - return arr.slice(num*-1); + if (num === 0) { + return [] + } + return arr.slice(num*-1); } function union(){ -// 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]); - } + // 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; + } + } + return result; } function zipObject(keysArr, valuesArr){ - var result = {}; - for (var i = 0; i < keysArr.length; i++) { - result[keysArr[i]] = valuesArr[i]; - } - return result; + 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) { + // 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 true; } - } - return false; -} -return collection.slice(fromIndex).indexOf(val) !== -1 ? true : false; + } + return false; + } + return collection.slice(fromIndex).indexOf(val) !== -1 ? true : false; } 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; + return arr[Math.floor(Math.random()*arr.length-1)]; +} + +function cloneDeep(collection){ // [1, 2, 3, [4, [5]]] + 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(arrOfObj, iteratee){ - if (typeof iteratee === "string") { - return arrOfObj.reduce(function(acc, obj){ - acc += obj[iteratee]; - return acc; - }, 0); - } else { - var sum = 0; - for (var i = 0; i < arrOfObj.length; i++) { - sum += iteratee(arrOfObj[i]); - } - return sum; - } + if (typeof iteratee === "string") { + return arrOfObj.reduce(function(acc, obj){ + acc += obj[iteratee]; + return acc; + }, 0); + } else { + var sum = 0; + for (var i = 0; i < arrOfObj.length; i++) { + sum += iteratee(arrOfObj[i]); + } + return sum; + } } function inRange(num, start=0, end){ -// Checks if n is between start and up to, but not including, end. -// If end is not specified, it's set to start with start then set to 0. -// If start is greater than end the params are swapped to support negative ranges. - if (end === undefined) { - end = start; - start = 0; - } - if (start > end) { - var hold = start; - start = end; - end = hold; - } - return (num > start && num < end); + // Checks if n is between start and up to, but not including, end. + // If end is not specified, it's set to start with start then set to 0. + // If start is greater than end the params are swapped to support negative ranges. + if (end === undefined) { + end = start; + start = 0; + } + if (start > end) { + var hold = start; + start = end; + end = hold; + } + return (num > start && num < end); } -function has(){ +function has(obj, path){ + // NOT WORKING + // returns true if obj.path exists + // returns false if obj.path is not valid + // has({a:1}, a) --> true + // has({{a:1}}, a) --> false + // if path is just a string + if (typeof path === "string") { + for (var key in obj) { + if (key === path) return true; + } + } + if (Array.isArray(path)) { + for (var i = 0; i < path.length; i++) { + if (obj[path[i]] === undefined) { + return false; + } + } + return true; + } + return false; } function omit(obj, arrKeysToOmit){ - var result = {}; - for (var key in obj) { - if (arrKeysToOmit.indexOf(key) === -1) { - result[key] = obj[key]; - } - } - return result; + var result = {}; + for (var key in obj) { + if (arrKeysToOmit.indexOf(key) === -1) { + result[key] = obj[key]; + } + } + return result; } function pick(obj, arrKeysToPick){ - var result = {}; - for (var key in obj) { - if (arrKeysToPick.indexOf(key) !== -1) { - result[key] = obj[key]; - } - } - return result; + var result = {}; + for (var key in obj) { + if (arrKeysToPick.indexOf(key) !== -1) { + result[key] = obj[key]; + } + } + return result; } function pickBy(obj, callback){ - var result = {}; - for (var key in obj) { - if (callback(obj[key])) { - result[key] = obj[key]; - } - } - return result; + var result = {}; + for (var key in obj) { + if (callback(obj[key])) { + result[key] = obj[key]; + } + } + return result; } function omitBy(obj, callback){ - var result = {}; - for (var key in obj) { - if (!callback(obj[key])) { - result[key] = obj[key]; - } - } - return result; + var result = {}; + for (var key in obj) { + if (!callback(obj[key])) { + result[key] = obj[key]; + } + } + return result; } -function padEnd(str="", length=0, chars=" "){ - //NOT WORKING - var result = str.slice(); - if (length === 0) { - result result; - } - return result; +function padEnd(str, finalLength, chars=" "){ + var result = str.slice(); + // if the desired length is less than or equal to the original string's length + // there is nothing to pad to the end, so return it. + // or if we pass in an empty string to pad to the end + if (str.length >= finalLength || chars.length === 0) { + return result; + } + var times = Math.floor((finalLength-str.length)/chars.length); + var remainder = (finalLength-str.length)%chars.length; + for (let i = 0; i < times; i++) { + // so I learned today that .concat() doesn't modify the original string + // and that you need to reassign it to capture the returned new string + // so I'm just going to use += from now on... + result += chars; + } + for (let j = 0; j < remainder; j++) { + result += chars[j]; + } + return result; } function repeat(str, times){ - if (times === 0) return ""; - if (times === 1) return str; + if (times === 0) return ""; + if (times === 1) return str; - var result = ""; - for (var i = 0; i < times; i++) { - result = result.concat(str); - } - return result; + var result = ""; + for (var i = 0; i < times; i++) { + result = result.concat(str); + } + return result; } function upperFirst(str){ - var result = ""; - return result.concat(str[0].toUpperCase() + str.slice(1)); + var result = ""; + return result.concat(str[0].toUpperCase() + str.slice(1)); } function flatten(arr){ - //NOT WORKING - var result = []; - for (var i = 0; i < arr.length; i++) { - if (Array.isArray(arr[i])) { - flatten(arr[i]); - } else { - result.push(arr[i]); - } - } - return result; + var result = []; + for (var i = 0; i < arr.length; i++) { + if (Array.isArray(arr[i])) { + // if the current element is an array, + // we need to go down a layer to flatten that as well + // and then remember to add it to the result and reassign since concat doesn't modify original arr + result = result.concat(flatten(arr[i])); + } else { + result.push(arr[i]); + } + } + return result; } // BONUS @@ -222,3 +257,4 @@ function flip(){ function flattenDeep(){ } + From 069f8f68422048177708ffa5dc1b35016c7bdb52 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Sat, 5 Aug 2017 15:31:34 -0700 Subject: [PATCH 25/32] flatten and flattenDeep --- lodash_exercise/lodash.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 902eac86..fdb39b88 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -228,14 +228,7 @@ function upperFirst(str){ function flatten(arr){ var result = []; for (var i = 0; i < arr.length; i++) { - if (Array.isArray(arr[i])) { - // if the current element is an array, - // we need to go down a layer to flatten that as well - // and then remember to add it to the result and reassign since concat doesn't modify original arr - result = result.concat(flatten(arr[i])); - } else { - result.push(arr[i]); - } + result = result.concat(arr[i]); } return result; } @@ -255,6 +248,17 @@ function flip(){ } function flattenDeep(){ - + var result = []; + for (var i = 0; i < arr.length; i++) { + if (Array.isArray(arr[i])) { + // if the current element is an array, + // we need to go down a layer to flatten that as well + // and then remember to add it to the result and reassign since concat doesn't modify original arr + result = result.concat(flatten(arr[i])); + } else { + result.push(arr[i]); + } + } + return result; } From c1c102dca2107a4b217b1e90495e49c4427d76bc Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Sat, 5 Aug 2017 16:53:02 -0700 Subject: [PATCH 26/32] zip --- lodash_exercise/lodash.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index fdb39b88..c3121f7c 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -235,9 +235,29 @@ function flatten(arr){ // BONUS +// passes in X number of arrays +// first array containing first elements, 2nd containing 2nd, etc. +// returns X number of arrays with those elements as listed +// e.g. [a,b,c], [1,2,3], [x,y,z] ==> [[a,1,x], [b,2,y], [c,3,z]] function zip(){ + var result = []; + for (let n = 0; n < arguments[0].length; n++) { + result.push([]); + } + // there should be N nested arrays for N elements in each array passed in + // e.g. if first array has 10 elements, resulting number of nested arrays is 10 -} + // loop through the arguments (they are all arrays) + // grab first/i element of each one, add to a new array + // add that new array to the result + for (let i = 0; i < arguments[0].length; i++) { + for (let j = 0; j < arguments.length; j++) { + result[i].push(arguments[j][i]); + } + } + return result; +} // note that this will only work if the longest array in the arguments is the 1st one passed in +// otherwise we would have to first find the longest array from the arguments function unzip(){ From 97f33d15b61099a7e6846e1b70ad500ad41c9b8b Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Sat, 5 Aug 2017 17:10:30 -0700 Subject: [PATCH 27/32] flattenDeep, unzip --- lodash_exercise/lodash.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index c3121f7c..eb27ae70 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -259,22 +259,38 @@ function zip(){ } // note that this will only work if the longest array in the arguments is the 1st one passed in // otherwise we would have to first find the longest array from the arguments -function unzip(){ +function unzip(){ // unzip takes in a result in the format of what's returned from zip + // e.g. [['a', 1, true], ['b', 2, false]], which has a length of 1 + var result = []; + // there should be N nested arrays for N elements in each arg + for (let n = 0; n < arguments[0][0].length; n++) { + result.push([]); + } + + // loop through the arguments (they are all arrays) + // grab first/i element of each one, add to a new array + // add that new array to the result + for (let i = 0; i < result.length; i++) { + for (let j = 0; j < arguments[0].length; j++) { + result[i].push(arguments[0][j][i]); + } + } + return result; } function flip(){ } -function flattenDeep(){ +function flattenDeep(arr){ var result = []; for (var i = 0; i < arr.length; i++) { if (Array.isArray(arr[i])) { // if the current element is an array, // we need to go down a layer to flatten that as well // and then remember to add it to the result and reassign since concat doesn't modify original arr - result = result.concat(flatten(arr[i])); + result = result.concat(flattenDeep(arr[i])); } else { result.push(arr[i]); } From d3084ba64d9c9e0142ad795e705dbad8e0990b8c Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Mon, 7 Aug 2017 17:14:35 -0700 Subject: [PATCH 28/32] finished hacker news clone --- jquery_exercise/index.html | 52 +++++++++++++++++++++++++++++++++++ jquery_exercise/script.js | 55 ++++++++++++++++++++++++++++++++++++++ jquery_exercise/style.css | 50 ++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 jquery_exercise/index.html create mode 100644 jquery_exercise/script.js create mode 100644 jquery_exercise/style.css diff --git a/jquery_exercise/index.html b/jquery_exercise/index.html new file mode 100644 index 00000000..1380eae3 --- /dev/null +++ b/jquery_exercise/index.html @@ -0,0 +1,52 @@ + + + + + Hacker News Clone + + + + +
+
+
+ Hack or Snooze + submit | favorites +
+
+
+
+ + +
+
+ + +
+ +
+ + +
+ + + + + \ No newline at end of file diff --git a/jquery_exercise/script.js b/jquery_exercise/script.js new file mode 100644 index 00000000..0eee8a7b --- /dev/null +++ b/jquery_exercise/script.js @@ -0,0 +1,55 @@ +$(document).ready(function(){ + var $submitLink = $(".submit"); + var $submitForm = $(".submit-form"); + var $submitBtn = $(".submit-button"); + var $linkItemsOl = $(".link-items"); + var star = ""; + var $title = $("#title"); + var $url = $("#url"); + var $favSpan = $(".favorites"); + + $submitLink.on("click", function(){ + $submitForm.toggle(); + }); + + $("body").on("click", ".glyphicon", function(e){ + $(e.target).toggleClass("glyphicon-star-empty glyphicon-star"); + }); + + $submitBtn.on("click", function(e){ + e.preventDefault(); + var title = $("#title").val(); + var url = $("#url").val(); + var $newItem = $("
  • "); + var aTag = ` ${title}`; + $newItem.html(star + " " + aTag); + $linkItemsOl.append($newItem); + console.log(aTag); + $title.val(""); + $url.val("https://"); + }); + + $favSpan.on("click", function(e){ + var stars = $(".glyphicon"); + if ($favSpan.hasClass("favorites")) { + for (let i = 0; i < stars.length; i++) { + console.log(stars[i]); + if ($(stars[i]).hasClass("glyphicon-star-empty")) { + $(stars[i]).parent().hide(); + } + } + $favSpan.text("all"); + } + else if ($favSpan.hasClass("all")) { + for (let i = 0; i < stars.length; i++) { + console.log(stars[i]); + if ($(stars[i]).hasClass("glyphicon-star-empty")) { + $(stars[i]).parent().show(); + } + } + $favSpan.text("favorites"); + } + $favSpan.toggleClass("favorites all"); + }); + +}); \ No newline at end of file diff --git a/jquery_exercise/style.css b/jquery_exercise/style.css new file mode 100644 index 00000000..ff6065ee --- /dev/null +++ b/jquery_exercise/style.css @@ -0,0 +1,50 @@ +.title, .submit, .favorites { + height: 3em; + margin: 5px; +} + +.submit:hover, .favorites:hover, .all:hover { + text-decoration: underline; + cursor: pointer; +} + +.glyphicon:hover { + cursor: pointer; +} + +.title { + font-size: 1.5em; + font-weight: bold; +} + +.wrapper { + background-color: #f6f6ef; + width: 80%; + margin-top: 1em; + margin-left: auto; + margin-right: auto; + color: #828282; +} + + +.panel > .panel-heading, .custom-panel { + background-image: none; + background-color: rgb(255, 102, 0); + color: black; + border: 1px solid rgb(255, 102, 0); +} + +.custom-form { + width: 30%; + margin-left: 5em; + margin-bottom: 2em; + display: none; +} + +a, a:hover { + color: black; +} + +.link-items { + margin-bottom: 50px; +} \ No newline at end of file From 19b05ab1f4454d7ec7b1f999593556c63961d08a Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Mon, 7 Aug 2017 17:15:14 -0700 Subject: [PATCH 29/32] finished lodash exercises and recursion stringifyNumbers --- lodash_exercise/lodash.js | 42 ++++++++++++++++++++++----------- lodash_exercise/lodashSpec.js | 3 +++ recursion_exercise/recursion.js | 18 ++++++++++++-- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index eb27ae70..6e2206b2 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -123,29 +123,33 @@ function inRange(num, start=0, end){ return (num > start && num < end); } -function has(obj, path){ - // NOT WORKING - +function has(obj, path) { // returns true if obj.path exists // returns false if obj.path is not valid - // has({a:1}, a) --> true - // has({{a:1}}, a) --> false - // if path is just a string + + // if path is just a string, just look for immediate key if (typeof path === "string") { for (var key in obj) { if (key === path) return true; } } + + // if path is an array, find if first element is immediate key + // then do again if there's a deeper layer if (Array.isArray(path)) { - for (var i = 0; i < path.length; i++) { - if (obj[path[i]] === undefined) { - return false; + // if path array only has one element + if (path.length === 1) { + for (var key in obj) { + if (key === path[0]) return true; } + return false; + } + // if the path array contains more than one element + for (var key in obj) { + if (key === path[0]) return has(obj[path[0]], path.slice(1)); } - return true; } - return false; -} +} function omit(obj, arrKeysToOmit){ var result = {}; @@ -279,8 +283,18 @@ function unzip(){ // unzip takes in a result in the format of what's returned fr return result; } -function flip(){ - +function flip(callback){ + // callback(arguments) ==> run stuff in callback on arguments.reverse + return function() { + // grab arguments and reverse them + var args = []; + for (var i = 0; i < arguments.length; i++) { + args.push(arguments[i]); + } + args.reverse(); + // spread out arguments so they're not in an array anymore and run through callback + return callback(...args); + } } function flattenDeep(arr){ diff --git a/lodash_exercise/lodashSpec.js b/lodash_exercise/lodashSpec.js index f0e90e9c..fcd548c8 100644 --- a/lodash_exercise/lodashSpec.js +++ b/lodash_exercise/lodashSpec.js @@ -161,6 +161,9 @@ describe("#has", function(){ it("should handle an array of keys as a parameter as well", function(){ expect(has(object, ['a', 'b'])).to.equal(true) }); + it("should return true if path is valid even with nested objects and >1 keys", function(){ + expect(has({'x': {}, 'a': {'b':3, 'c':4}, 'b': 3}, ['a','c'])).to.equal(true) + }); }); diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index 1322f5f4..4d8bb610 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -58,6 +58,20 @@ function binarySearch(arr, val) { // repeat } -function stringifyNumbers(obj) { - +function stringifyNumbers(obj, newObj={}) { +// Write a function called `stringifyNumbers` which takes in an object and +// finds all of the values which are numbers and converts them to strings. +// Return new obj with all numbers as strings + for (var key in obj) { + if (typeof obj[key] === "number") { + newObj[key] = obj[key].toString(); + } + else if (typeof obj[key] === "object" && Array.isArray(obj[key]) === false) { + newObj[key] = stringifyNumbers(obj[key]); + } + else { + newObj[key] = obj[key]; + } + } + return newObj; } \ No newline at end of file From c39642a8b32ba4831d240fa4532e45ed81cc9f32 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Wed, 9 Aug 2017 17:37:01 -0700 Subject: [PATCH 30/32] finished call apply bind exercises - ready for review --- call_apply_bind_exercise/callApplyBind.js | 55 +++++++++++++++++++++++ call_apply_bind_exercise/readme.md | 15 +++++-- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/call_apply_bind_exercise/callApplyBind.js b/call_apply_bind_exercise/callApplyBind.js index e69de29b..da9ed2cb 100644 --- a/call_apply_bind_exercise/callApplyBind.js +++ b/call_apply_bind_exercise/callApplyBind.js @@ -0,0 +1,55 @@ +function sumEvenArguments() { + var arr = [].slice.call(arguments); + var sum = 0; + for (let i = 0; i < arr.length; i++) { + if (arr[i] % 2 === 0) { + sum += arr[i]; + } + } + return sum; +} + +function arrayFrom(arrLikeObj) { + var arr = [].slice.call(arrLikeObj); + return arr; +} + +function invokeMax(fn, maxTimes) { + var counter = 0; + return function() { + if (counter >= maxTimes) { + return "Maxed Out!"; + } else { + counter++; + return fn.apply(this, arguments); + } + } +} + +function guessingGame(num) { + var answer = Math.floor(Math.random()*10); + var guesses = 0; + return function(guess) { + if (guesses === num) { + return `No more guesses. The answer was ${answer}.`; + } + else { + guess++; + if (guess === answer) { + return "You got it!"; + } + else if (guess > answer) { + return "You're too high!"; + } + else if (guess < answer) { + return "You're too low!"; + } + } + } +} + + + + + + diff --git a/call_apply_bind_exercise/readme.md b/call_apply_bind_exercise/readme.md index cbc3cd03..7a353703 100644 --- a/call_apply_bind_exercise/readme.md +++ b/call_apply_bind_exercise/readme.md @@ -3,19 +3,28 @@ Fix the following code: ```javascript +// var obj = { +// fullName: "Harry Potter", +// person: { +// sayHi: function(){ +// return "This person's name is " + this.fullName +// } +// } +// } + var obj = { fullName: "Harry Potter", person: { sayHi: function(){ return "This person's name is " + this.fullName - } + }.apply(obj) } } ``` - List two examples of "array-like-objects" that we have seen. - - - - + - arguments + - result of DOM method such as getElementsByTagName ### Functions to write: From 32845480d0505a557daa41199025cbb67c59b4d8 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Thu, 10 Aug 2017 16:23:32 -0700 Subject: [PATCH 31/32] finished parts 1 and 2 for prototypes exercises - ready for review --- prototypes_exercise/prototypes.js | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/prototypes_exercise/prototypes.js b/prototypes_exercise/prototypes.js index e69de29b..10cf59bb 100644 --- a/prototypes_exercise/prototypes.js +++ b/prototypes_exercise/prototypes.js @@ -0,0 +1,53 @@ +// PART 1 + +function Person(firstName, lastName, favoriteColor, favoriteNumber, favoriteFoods=[]) { + this.firstName = firstName; + this.lastName = lastName; + this.favoriteColor = favoriteColor; + this.favoriteNumber = favoriteNumber; + this.favoriteFoods = favoriteFoods; + this.family = []; +} + +Person.prototype.fullName = function() { + return `${this.firstName} ${this.lastName}`; +} + +Person.prototype.toString = function() { + return `${this.firstName} ${this.lastName}, Favorite Color: ${this.favoriteColor}, Favorite Number: ${this.favoriteNumber}`; +} + +Person.prototype.addToFamily = function(personObj) { + // check if personObj is actually a Person object & if there is a duplicate + if (personObj instanceof Person && this.family.indexOf(personObj) === -1) { + this.family.push(personObj); + } + return this.family.length; +} + +// PART 2 + +Array.prototype.map = function(fn) { + var result = []; + for (let i = 0; i < this.length; i++) { + result.push(fn(this[i], i, this)); + } + return result; +} + +String.prototype.reverse = function() { + var result = []; + let temp = this.split(""); + for (let i = temp.length-1; i >= 0; i--) { + result.push(temp[i]); + } + return result.join(""); +} + +Function.prototype.bind = function(thisArg, ...outerArgs) { // anon func takes thisArg to reset the this, and other arguments if needed + var _this = this; // refers to the function that bind was called on + // _this takes whatever arguments that it usually would + return function(...innerArgs) { + return _this.apply(thisArg, outerArgs.concat(innerArgs)); + } +} \ No newline at end of file From a4e98264652b6881b1037d6ec133feb4adf2acf1 Mon Sep 17 00:00:00 2001 From: Michelle Huynh Date: Mon, 14 Aug 2017 16:58:26 -0700 Subject: [PATCH 32/32] incomplete hack or snooze. Need to work on implementing signup/login and favorites --- ajax_with_jquery_exercise/favorites.html | 0 ajax_with_jquery_exercise/index.html | 66 +++++++++++ ajax_with_jquery_exercise/script.js | 135 +++++++++++++++++++++++ ajax_with_jquery_exercise/style.css | 58 ++++++++++ recursion_exercise/recursion.js | 16 ++- 5 files changed, 273 insertions(+), 2 deletions(-) create mode 100644 ajax_with_jquery_exercise/favorites.html create mode 100644 ajax_with_jquery_exercise/index.html create mode 100644 ajax_with_jquery_exercise/script.js create mode 100644 ajax_with_jquery_exercise/style.css diff --git a/ajax_with_jquery_exercise/favorites.html b/ajax_with_jquery_exercise/favorites.html new file mode 100644 index 00000000..e69de29b diff --git a/ajax_with_jquery_exercise/index.html b/ajax_with_jquery_exercise/index.html new file mode 100644 index 00000000..d2ae82d5 --- /dev/null +++ b/ajax_with_jquery_exercise/index.html @@ -0,0 +1,66 @@ + + + + + Hacker News Clone + + + + +
    +
    +
    + Hack or Snooze + submit | favorites + +
    +
    + + + + + +
    + + + + + \ No newline at end of file diff --git a/ajax_with_jquery_exercise/script.js b/ajax_with_jquery_exercise/script.js new file mode 100644 index 00000000..af7e18d0 --- /dev/null +++ b/ajax_with_jquery_exercise/script.js @@ -0,0 +1,135 @@ +$(document).ready(function(){ + var star = ""; + var $linkItemsOl = $(".link-items"); + + $.ajax({ + method: "GET", + url: "https://hacker-news.firebaseio.com/v0/topstories.json?" + }).then(function(topItems){ + var top20 = topItems.slice(0,20); + for (let i = 0; i < top20.length; i++) { + $.ajax({ + method: "GET", + dataType: "json", + url: `https://hacker-news.firebaseio.com/v0/item/${top20[i]}.json?print=pretty` + }).then(function(item){ + addItemToList(item.title, item.url); + console.log(item.title + '\n' + item.url); + //item.title + //item.url + }); + + } + }); + + function addItemToList(itemTitle, itemUrl) { + var $newItem = $("
  • ", { "class" : "item"}); + var aTag = ` ${itemTitle}`; + $newItem.html(star + " " + aTag); + $linkItemsOl.append($newItem); + console.log(aTag); + } + + $(".login").on("click", function(e) { + $(".login-form").toggle(); + $(".signup-form").toggle(); + }); + + $("login-signup-form").on("click", "login-button", function(e){ + e.preventDefault(); + var email = $("#login-email").val(); + var pw = $("#login-pw").val(); + $.ajax({ + method: "POST", + url: "https://hn-favorites.herokuapp.com/login", + //Content-Type: "application/json" + data: { + "email" : email, + "password": pw + } + }).then(function(authKey){ + console.log(authKey); + }); + }); + + $("#signup-form").on("submit", function(e){ + e.preventDefault(); + var email = $("#signup-email").val(); + var pw = $("#signup-pw").val(); + console.log(email + pw); + $.ajax({ + method: "POST", + url: "https://hn-favorites.herokuapp.com/signup", + data: { + "email" : email, + "password": pw + } + }).then(function(authKey){ + console.log(authKey); + }); + }); + + // what to do with the authkey? + // you need it to: + // GET all favorites + // DELETE a favorite + + // when you click the log in button + // post to log in + // + + + // var $submitLink = $(".submit"); + // var $submitForm = $(".submit-form"); + // var $submitBtn = $(".submit-button"); + // var $linkItemsOl = $(".link-items"); + // var star = ""; + // var $title = $("#title"); + // var $url = $("#url"); + // var $favSpan = $(".favorites"); + + // $submitLink.on("click", function(){ + // $submitForm.toggle(); + // }); + + // $("body").on("click", ".glyphicon", function(e){ + // $(e.target).toggleClass("glyphicon-star-empty glyphicon-star"); + // }); + + // $submitBtn.on("click", function(e){ + // e.preventDefault(); + // var title = $("#title").val(); + // var url = $("#url").val(); + // var $newItem = $("
  • "); + // var aTag = ` ${title}`; + // $newItem.html(star + " " + aTag); + // $linkItemsOl.append($newItem); + // console.log(aTag); + // $title.val(""); + // $url.val("https://"); + // }); + + // $favSpan.on("click", function(e){ + // var stars = $(".glyphicon"); + // if ($favSpan.hasClass("favorites")) { + // for (let i = 0; i < stars.length; i++) { + // console.log(stars[i]); + // if ($(stars[i]).hasClass("glyphicon-star-empty")) { + // $(stars[i]).parent().hide(); + // } + // } + // $favSpan.text("all"); + // } + // else if ($favSpan.hasClass("all")) { + // for (let i = 0; i < stars.length; i++) { + // console.log(stars[i]); + // if ($(stars[i]).hasClass("glyphicon-star-empty")) { + // $(stars[i]).parent().show(); + // } + // } + // $favSpan.text("favorites"); + // } + // $favSpan.toggleClass("favorites all"); + // }); + +}); \ No newline at end of file diff --git a/ajax_with_jquery_exercise/style.css b/ajax_with_jquery_exercise/style.css new file mode 100644 index 00000000..c8bf8089 --- /dev/null +++ b/ajax_with_jquery_exercise/style.css @@ -0,0 +1,58 @@ +.item { + margin-bottom: 10px; +} + +.title, .submit, .favorites { + height: 3em; + margin: 5px; +} + +.submit:hover, .favorites:hover, .all:hover, .login:hover, .signup:hover { + text-decoration: underline; + cursor: pointer; +} + +.login { + margin-left: 63%; +} + +.glyphicon:hover { + cursor: pointer; +} + +.title { + font-size: 1.5em; + font-weight: bold; +} + +.wrapper { + background-color: #f6f6ef; + width: 80%; + margin-top: 1em; + margin-left: auto; + margin-right: auto; + color: #828282; +} + + +.panel > .panel-heading, .custom-panel { + background-image: none; + background-color: rgb(255, 102, 0); + color: black; + border: 1px solid rgb(255, 102, 0); +} + +/*.custom-form { + width: 15%; + margin-left: 68em; + margin-bottom: 2em; + display: none; +}*/ + +a, a:hover { + color: black; +} + +.link-items { + margin-bottom: 50px; +} \ No newline at end of file diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index 4d8bb610..2be46951 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -51,9 +51,21 @@ function search(arr, val, currentIndex=0) { } function binarySearch(arr, val) { - // sort the array + // sort the array, or assume sorted // find the middle, compare - // remove the middle and everything before or after it + // need some value to store current index of the original array + var pivot = Math.floor(arr.length/2); + if (arr[pivot] === val) { + return pivot; + } + if (arr[pivot] > val) { + return binarySearch(arr.slice(0, pivot), val); + } + if (arr[pivot] < val) { + return binarySearch(arr.slice(pivot), val); + } + return -1; + // remove the middle and everything before or after it, // depending on the comparison // repeat }