From 69906a610c3b3783fe9bdbe6fa3113e2319cac3b Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Mon, 31 Jul 2017 19:05:54 -0700 Subject: [PATCH 01/10] Testing Exercise Commit --- testing_exercise/testing.js | 39 +++++++++++++++++++++++++++ testing_exercise/testingSpec.js | 48 ++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index e69de29b..02f0eaa3 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -0,0 +1,39 @@ +function replaceWith(str, charToFind, charReplacement) { + var find = charToFind; + var re = new RegExp(find, 'g'); + return str.replace(re, charReplacement) +} + + +function expand(arr, num) { + var finalArr = []; + for (var i = 0; i < num; i++) { + finalArr = finalArr.concat(arr); + } + return finalArr; +} + +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) { + var singleArr = arr1.concat(arr2); + var finalArr = singleArr.sort(); + return finalArr; +} + +function mergeObjects(obj1, obj2) { + var finalObj = obj1; + for(var n in obj2) { + finalObj[n] = obj2[n]; + } + return finalObj +} + + diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index aef56b1d..a3260953 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -1,3 +1,49 @@ + + +// WRITE YOUR TESTS HERE! + +//replaceWith test var expect = chai.expect; -// WRITE YOUR TESTS HERE! \ No newline at end of file +describe("case sensitve letter replacement", function() { + it("replaces charToFind with charReplacement", function() { + expect(replaceWith('Foo', 'F', 'B')).to.equal('Boo'); + expect(replaceWith("awesome", "e", "z")).to.equal("awzsomz") + }) +}) + +//expand test +describe("concatenation of arrays", function() { + it("returns an array that has been copied", function() { + expect(expand([1,2,3], 3)).to.deep.equal([1,2,3,1,2,3,1,2,3]); + expect(expand(["foo", "test"], 1)).to.deep.equal(["foo","test"]); + }) +}) + +//acceptNumbersOnly test +describe("confirm if all arguments are numbers", function() { + it("returns a boolean indicating if all args are numbers", function() { + expect(acceptNumbersOnly(1,2,3,4,5,6,7)).to.equal(true); + expect(acceptNumbersOnly(1,2,3,4,5,6,NaN)).to.equal(false); + }) +}) + +//mergeArrays test +describe("takes in two arrays", function() { + it("returns a sorted array", function() { + expect(mergeArrays([2,1],[3,4])).to.deep.equal([1,2,3,4]); + }) +}) + +//mergeObjects test +describe("takes in two objects", function() { + it("returns one object with all properties", function() { + expect(mergeObjects({name: "Foo", num: 33}, {test: "thing", num: 55})).to.deep.equal({name: "Foo", test: "thing", num: 55}) + }) +}) + + + + + + From d74b388d70b8397a68632ec3f58cf5a6333f7074 Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Wed, 2 Aug 2017 06:46:33 -0700 Subject: [PATCH 02/10] recursion exercises --- recursion_exercise/recursion.js | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index e69de29b..3d5259ff 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -0,0 +1,75 @@ +//productOfArray +function productOfArray(arr) { + if(arr.length === 0) { + return 1; + } + return arr[0] * productOfArray(arr.slice(1)); +} + +//collectStrings +function collectStrings(obj) { + + var finalArray = []; + + function collectStringsHelper(objH) { + + for(var n in objH) { + + if(typeof objH[n] === 'string') { + finalArray.push(objH[n]); + } else { + return collectStringsHelper(objH[n]) + } + } + + } + collectStringsHelper(obj); + return finalArray; + +} + +//contains +//input: nested object +//output: boolean +//process: recurse thru a nested object and look for a value +function contains(obj, val) { + for (var key in obj) { + if (obj[key] === val) { + return true; + } + + if (typeof obj[key] === 'object') { + if (contains(obj[key], val)) { + return true; + } + } + } + + return false; +} + + +function realSize(arrays) { + // Force be with you, code warrior! + var count = 0; + + for(var i = 0; i < arrays.length + if(Array.isArray(arrays)) { + return realSize(arra + } + + return 0; +} + + + + + + + + + + + + + From 7f2a1b1767a50e9515193c6f5d59a888ac0200f5 Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Thu, 3 Aug 2017 08:48:46 -0700 Subject: [PATCH 03/10] 080317 Commits --- lodash_exercise/lodash.js | 222 +++++++++++++++++++++++++------- recursion_exercise/recursion.js | 12 +- 2 files changed, 177 insertions(+), 57 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 483d734a..719823be 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,57 +1,187 @@ -function drop(){ +function drop(arr, num){ + var dropArr = []; -} - -function fromPairs(){ - -} - -function head(){ - -} - -function take(){ - -} - -function takeRight(){ - -} - -function union(){ - -} - -function zipObject(){ + if(num === undefined) { + num = 1; + } + for(var i = num; i < arr.length; i++) { + dropArr.push(arr[i]) + } + return dropArr; + } -function includes(){ - -} - -function sample(){ - -} - -function cloneDeep(){ - -} +function fromPairs(arr){ + var pairsObj = {}; -function sumBy(){ + for(var i = 0; i < arr.length; i++) { + pairsObj[arr[i][0]] = arr[i][1]; + } + return pairsObj; } -function inRange(){ - -} - -function has(){ - -} - -function omit(){ - +function head(){ + if(arguments[0].length === 0) { + return undefined + } else { + return arguments[0][0]; + } + +} + +function take(arr, num){ + var takeArr = []; + if(arr.length === 0) { + return takeArr; + } + if(num === undefined) { + takeArr.push(arr[0]); + } + if(num > arr.length) { + num = arr.length; + } + + for(var i = 0; i < num; i++) { + takeArr.push(arr[i]); + } + return takeArr; + +} + +function takeRight(arr, num){ + var arrRt = []; + if(num === 0) { + return arrRt; + } + if(num === undefined) { + arrRt.push(arr[arr.length-1]); + } + if(num > arr.length) { + num = 1 + } + for(var i = num-1; i < arr.length; i++) { + arrRt.push(arr[i]); + } + + return arrRt; + +} + +function union(arrays){ + + var uniqArr = []; + var singleArr = []; + + for(var i = 0; i < arguments.length; i++) { + singleArr = singleArr.concat(arguments[i]) + } + for(var i = 0; i < singleArr.length; i++) { + if(uniqArr.indexOf(singleArr[i]) === -1) { + uniqArr.push(singleArr[i]); + } + } + return uniqArr; + +} + +function zipObject(arr1, arr2){ + var zipObj = {}; + for(var i = 0; i < arr1.length; i++) { + zipObj[arr1[i]] = arr2[i]; + } + return zipObj; +} + +//Checks if value is in collection. If collection is a string, it's checked for a substring of value, otherwise SameValueZero is used for equality comparisons. If fromIndex is negative, it's used as the offset from the end of collection. +function includes(collection, value, indx){ +//REFACTOR + + if(indx !== undefined) { + startPt = indx; + } else { + startPt = 0; + } + if(typeof collection === 'string') { + if(collection.indexOf(value, startPt) !== -1) { + return true; + } + return false + } else if(Array.isArray(collection)) { + if(collection.indexOf(value, startPt) !== -1) { + return true + } + return false + } else { + for (var n in collection) { + if(collection[n] === value) { + return true; + } + } + } +} + +function sample(arr){ + var randomIndex = Math.floor(Math.random() * arr.length); + return arr[randomIndex]; +} + +function cloneDeep(collection){ + var copyOf = JSON.parse(JSON.stringify(collection)); + return copyOf; +} + +function sumBy(arr, param2){ + // var total = 0; + + // for(var i = 0; i < arr.length; i++) { + // if(typeof param2 {} === 'function') { + // total += param2 + // } + // total += callback(arr[i]); + // } + + // return total; +} + +function inRange(num, start, end){ + + if (end === undefined) { + end = start; + start = 0; + } + if (start > end) { + var holder = start; + start = end; + end = holder; + } + if(num >= start && num < end) { + return true; + } return false; + +} + +function has(obj, path){ + if(Array.isArray(path)){ + var arrPath = path.join("."); + if( obj+ arrPath === undefined) + return false; + return true; + } + + if(obj.hasOwnProperty(path)) + return true; + return false; +} + +function omit(obj, paths){ + //create a copy of obj + var copy = Object.assign({}, obj); + for(var i = 0; i < paths.length; i++) { + delete copy[paths[i]]; + } + return copy; } function pick(){ diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index 3d5259ff..7f6b9d5f 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -49,17 +49,7 @@ function contains(obj, val) { } -function realSize(arrays) { - // Force be with you, code warrior! - var count = 0; - - for(var i = 0; i < arrays.length - if(Array.isArray(arrays)) { - return realSize(arra - } - - return 0; -} + From f6bc5f7f5af7224b91c45cd41595e29859be1a7e Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Fri, 4 Aug 2017 08:28:36 -0700 Subject: [PATCH 04/10] Shapes exercise --- canvas_exercise/shapes_game/index.js | 165 +++++++++++++++++++++++-- canvas_exercise/shapes_game/styles.css | 1 + lodash_exercise/lodash.js | 10 +- 3 files changed, 162 insertions(+), 14 deletions(-) diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 0de5f18a..85fb7dcf 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -1,17 +1,5 @@ window.addEventListener("load", function() { - function clear(ctx, width, heigt) { - } - - function drawRandomShape(ctx, width, height) { - } - - function drawGameStartText(ctx, width, height, score) { - } - - function restartGame(ctx, width, height) { - } - var canvas = document.getElementById("shapes-game"), height = canvas.scrollHeight, width = canvas.scrollWidth, @@ -29,6 +17,159 @@ window.addEventListener("load", function() { canvas.width = width; canvas.height = height; + var shapeVal = 0; + var totalScore = 0; + var timerCountDown = 1; + + + +//------------------------------------------------------------------- + +function drawStartText() { + + var canvas = document.getElementById("shapes-game") + var ctx = canvas.getContext('2d'); + + ctx.font = '40px serif'; + ctx.fillStyle = 'orange'; + ctx.fillText('Press the Space Bar to start a new game', 60, 340); + +} + +function countDown() { + timerCountDown = 10; + var int = setInterval(function () { + timerSpan.innerHTML = timerCountDown; + timerCountDown-- || clearInterval(int); //if i is 0, then stop the interval + }, 1000); +} + + + +// function countDown(callback) { +// timerCountDown = 10; +// var int = setInterval(function () { +// timerSpan.innerHTML = timerCountDown; +// timerCountDown-- ; +// if(timerCountDown < 1) { +// learInterval(int); //if i is 0, then stop the interval +// callback(value) +// } +// }, 1000); +// } + + +//------------------------------------------------- + function clear() { + ctx.clearRect(0, 0, height, width); + } + + function startGame() { + drawRandomShape(); + // countDown(); + countDown(clear()); + } + + function drawRandomShape() { + var randomImg = Math.floor(Math.random()*(5-1)+1); + switch(randomImg) { + case 1: + drawRedSquare() + break; + case 2: + drawWhiteSquare() + break; + case 3: + drawRedTriangle() + break; + case 4: + drawWhiteTriangle() + } + + document.body.onkeydown = function(e){ + if(e.keyCode == shapeVal) { + totalScore++ + scoreSpan.innerHTML = totalScore; + clear() + drawRandomShape() + } else { + totalScore-- + scoreSpan.innerHTML = totalScore; + clear() + drawRandomShape() + } + } + } + + function drawRedSquare() { + shapeVal = 40 + // var ctx = canvas.getContext('2d'); + var upperLeftX = randomCoordinate() + var upperLeftY = randomCoordinate() + var width = 100; + var height = 100; + ctx.fillStyle = "red"; + ctx.fillRect(upperLeftX, upperLeftY, width, height); + } + + function drawWhiteSquare() { + shapeVal = 39 + // var ctx = canvas.getContext('2d'); + + var upperLeftX = randomCoordinate() + var upperLeftY = randomCoordinate() + var width = 100; + var height = 100; + ctx.fillStyle = "white"; + ctx.fillRect(upperLeftX, upperLeftY, width, height); + } + +function drawWhiteTriangle() { + shapeVal = 38 + + var xCoord = randomCoordinate() + var yCoord = randomCoordinate() + var canvas = document.getElementById('shapes-game'); + // var ctx = canvas.getContext('2d'); + ctx.fillStyle = "white"; + ctx.beginPath(); + ctx.moveTo(xCoord, yCoord); + ctx.lineTo(xCoord, yCoord + 100); + ctx.lineTo(xCoord + 100, yCoord + 100); + ctx.fill(); +} + +function drawRedTriangle() { + shapeVal = 37 + + var xCoord = randomCoordinate() + var yCoord = randomCoordinate() + var canvas = document.getElementById('shapes-game'); + // var ctx = canvas.getContext('2d'); + ctx.fillStyle = "red"; + ctx.beginPath(); + ctx.moveTo(xCoord, yCoord); + ctx.lineTo(xCoord, yCoord + 100); + ctx.lineTo(xCoord + 100, yCoord + 100); + ctx.fill(); +} + +function randomCoordinate() { + return Math.floor(Math.random()*(600-100)+100); +} + +document.body.onkeydown = function(e){ + if(e.keyCode == 32){ + startGame(); + drawStartText(); + + } +} + + + + + document.addEventListener("keyup", function() { }); diff --git a/canvas_exercise/shapes_game/styles.css b/canvas_exercise/shapes_game/styles.css index cbe4fa83..f66944ad 100644 --- a/canvas_exercise/shapes_game/styles.css +++ b/canvas_exercise/shapes_game/styles.css @@ -5,6 +5,7 @@ body { #shapes-game { border: 4px solid grey; background-color: black; + } .canvas-container { diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 719823be..6e8e9420 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -184,8 +184,14 @@ function omit(obj, paths){ return copy; } -function pick(){ - +function pick(obj, paths){ + var finalObj = {}; + + for(let val of paths) { + finalObj[val] = obj[val]; + } + return finalObj; + } function pickBy(){ From 70fd94174149b0bf1f365e5b6e1511926b92c12d Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Mon, 7 Aug 2017 05:57:00 -0700 Subject: [PATCH 05/10] Completed assignments - still working on bonus questions --- canvas_exercise/.DS_Store | Bin 0 -> 6148 bytes canvas_exercise/shapes_game/index.js | 154 ++++++++++++++------------- lodash_exercise/lodash.js | 137 +++++++++++++++++------- recursion_exercise/recursion.js | 17 +++ recursion_exercise/recursionSpec.js | 4 +- 5 files changed, 197 insertions(+), 115 deletions(-) create mode 100644 canvas_exercise/.DS_Store diff --git a/canvas_exercise/.DS_Store b/canvas_exercise/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c8afd7dd9e6697d371bc27bb7cc40b8dd5ee03ac GIT binary patch literal 6148 zcmeHKJ8r`;3?&;62C{hUs4MgaLXe)I2WXqxXodtS5Onv{bM%~^~`fO- len) { + str = str.substring(0, len); + } + if(sl < len) { + var loops = (len-sl)/chars.length; + if(loops % 2 === 0) { + for(var i = 0; i < loops; i++) { + str += chars; + } + } else { + for(var i = 0; i < Math.ceil((len-sl)/chars.length); i++) { + str += chars; + } + str = str.substring(0, len); + } + return str; + } } -function repeat(){ - +function repeat(str, num){ + var finalStr = ""; + if(num === undefined) { + return ""; + } + for(var i = 0; i < num; i++) { + finalStr += str; + } + return finalStr; } function upperFirst(str){ - + var result = str.split(""); + result[0] = result[0].toUpperCase(); + var finalStr = result.join(""); + return finalStr; } -function flatten(){ - +function flatten(array){ + var finalArr = [] + + function flattenHelper (arr) { + for(var i = 0; i < arr.length; i++) { + if(Array.isArray(arr[i])) { + for(var j = 0; j < arr[i].length; j++) { + finalArr.push(arr[i][j]); + } + } else { + finalArr.push(arr[i]); + } + } + return finalArr; + } + flattenHelper(array); + return finalArr; } function zip(){ diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index 7f6b9d5f..30dc81e8 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -49,6 +49,23 @@ function contains(obj, val) { } +function search(arr, val) { + var indx = 0; + function searchHelper(recArr, val) { + if(recArr[0] === val) { + return indx; + } else if(recArr.length > 1) { + indx++; + recArr.shift(); + searchHelper(recArr, val); + } else { + indx = -1 + return indx + } + } + searchHelper(arr, val); + return indx; +} 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 e462c6342a57fdc66bb5c2bd44583fffea42ef8f Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Tue, 8 Aug 2017 06:36:18 -0700 Subject: [PATCH 06/10] Includes basic functionality - will improve page this weekend. --- jquery_exercise/index.html | 85 ++++++++++++++++++++++++++++++++++++++ jquery_exercise/main.js | 25 +++++++++++ jquery_exercise/style.css | 0 3 files changed, 110 insertions(+) create mode 100644 jquery_exercise/index.html create mode 100644 jquery_exercise/main.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..8f4ffc17 --- /dev/null +++ b/jquery_exercise/index.html @@ -0,0 +1,85 @@ + + + + + Hacker News Clone + + + + + + + + + + + + + + + + + + + +
+
+
+

Hack or Snooze

+
+
+ submit +
+
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ + +
+
+
+ +
    +
+
+
+
+ +
+ + + + + + + + + + + + + + + + + + diff --git a/jquery_exercise/main.js b/jquery_exercise/main.js new file mode 100644 index 00000000..b2d76b37 --- /dev/null +++ b/jquery_exercise/main.js @@ -0,0 +1,25 @@ +$(function() { + + $("form").on("submit", function(e) { + e.preventDefault(); + + var $newTitle = $('#newTitle').val() + var $newURL = $('#urlToAdd').val() + var icon = "" + var $newLi = $("
  • " + icon + " " + $newTitle + " " + $newURL + "
  • "); + $("ol").append($newLi); + + $newTitle.val(""); + $newURL.val(""); + }); + + + $('ol').click(function(event){ + $(event.target).toggleClass('glyphicon-star-empty glyphicon-star'); + }); + +}); + + + + diff --git a/jquery_exercise/style.css b/jquery_exercise/style.css new file mode 100644 index 00000000..e69de29b From 6af22441a1224423035de9d68afbfa156f9e8a48 Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Tue, 8 Aug 2017 06:47:30 -0700 Subject: [PATCH 07/10] Implemented recommeneded changes --- canvas_exercise/shapes_game/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 8fd67930..e17c8dd7 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -33,7 +33,6 @@ drawStartText(); clear(); //clear the canvas drawRandomShape(); //add a random shape track scoring countDown(); //start timer countdown - endGaame() //use as a callback for when timer countdown expires } //------------------------------------------------------------------- @@ -102,11 +101,11 @@ function clear() { function countDown() { timerCountDown = 30; - var int = setInterval(function () { + var intervalID = setInterval(function () { timerSpan.innerHTML = timerCountDown; timerCountDown-- ; if(timerCountDown < 1) { - clearInterval(int); //if i is 0, then stop the interval + clearInterval(intervalID); //if i is 0, then stop the interval endGame(); totalScore = 0; if (event.onkeypress) { From 0ec0783eb423b6c2de4c11d6e409d8dad6239cd6 Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Thu, 10 Aug 2017 06:17:46 -0700 Subject: [PATCH 08/10] Call Bind Apply Exercise --- call_apply_bind_exercise/callApplyBind.js | 66 +++++++++++++++++++++++ call_apply_bind_exercise/readme.md | 10 ++-- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/call_apply_bind_exercise/callApplyBind.js b/call_apply_bind_exercise/callApplyBind.js index e69de29b..673fbc96 100644 --- a/call_apply_bind_exercise/callApplyBind.js +++ b/call_apply_bind_exercise/callApplyBind.js @@ -0,0 +1,66 @@ +function sumEvenArguments() { + var result = 0; + for(var i = 0; i < arguments.length; i++) { + if(arguments[i] % 2 === 0) { + result += arguments[i]; + } + } + return result +} + + +function arrayFrom() { + var finalArr = []; + for(var i = 0; i < arguments.length; i++) { + finalArr.push(arguments[i]); + } + return finalArr; +} + +function invokeMax(fn, maxAmount) { + + var counter = 0; + + return function() { + counter++; + + if(counter > maxAmount) { + return "Maxed Out!" + } else { + return fn.apply(this, [].slice.call(arguments)) + } + } +} + + +var addOnlyThreeTimes = invokeMax(add,3); +addOnlyThreeTimes(1,2); // 3 +addOnlyThreeTimes(2,2); // 4 +addOnlyThreeTimes(1,2); // 3 +addOnlyThreeTimes(1,2); // "Maxed Out!" + + + +function guessingGame(amount) { + + var answer = Math.round(Math.random()*10) + var guesses = 0; + + + return function(guess) { + if(guesses < amount) { + guesses++ + } else { + return "Game Over!" + } + + 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!" + } + + } +} \ No newline at end of file diff --git a/call_apply_bind_exercise/readme.md b/call_apply_bind_exercise/readme.md index cbc3cd03..38a30123 100644 --- a/call_apply_bind_exercise/readme.md +++ b/call_apply_bind_exercise/readme.md @@ -5,17 +5,15 @@ Fix the following code: ```javascript var obj = { fullName: "Harry Potter", - person: { - sayHi: function(){ - return "This person's name is " + this.fullName - } + sayHi: function(){ + return "This person's name is " + this.fullName; } } ``` - List two examples of "array-like-objects" that we have seen. - - - - + - arguments + - sets ### Functions to write: From 061737f9b502a369c3ac068c8024fecf46a54717 Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Mon, 14 Aug 2017 16:05:52 -0700 Subject: [PATCH 09/10] Hacker Snooze commit - currently includes functionality to authenticate users and to Post and Get favorites. --- ajax_with_jquery_exercise/index.html | 93 +++++++++++++++ ajax_with_jquery_exercise/main.js | 167 +++++++++++++++++++++++++++ 2 files changed, 260 insertions(+) create mode 100644 ajax_with_jquery_exercise/index.html create mode 100644 ajax_with_jquery_exercise/main.js diff --git a/ajax_with_jquery_exercise/index.html b/ajax_with_jquery_exercise/index.html new file mode 100644 index 00000000..442667f9 --- /dev/null +++ b/ajax_with_jquery_exercise/index.html @@ -0,0 +1,93 @@ + + + + + Hacker News Clone + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +

    Hack or Snooze

    +
    +
    + Log In +
    +
    + Sign Up +
    + + +
    + +
    +
    + + +
    + +
    + +
    +
    +
    + +
    + +
    + + +
    + +
    + +
    + + +
    +
    +
    + +
      +
    +
    +
    +
    + +
    + + + + + + + + + + + + + + + + + + diff --git a/ajax_with_jquery_exercise/main.js b/ajax_with_jquery_exercise/main.js new file mode 100644 index 00000000..679e21cb --- /dev/null +++ b/ajax_with_jquery_exercise/main.js @@ -0,0 +1,167 @@ +$(function() { + + $("form").on("submit", function(e) { + e.preventDefault(); + + topStories() + signUpUser() + + // var $newTitle = $('#newTitle').val() + // var $newURL = $('#urlToAdd').val() + // var icon = "" + // var $newLi = $("
  • " + icon + " " + $newTitle + " " + $newURL + "
  • "); + // $("ol").append($newLi); + + // $newTitle.val(""); + // $newURL.val(""); + }); + + + $('ol').click(function(event){ + $(event.target).toggleClass('glyphicon-star-empty glyphicon-star'); + + addToFavorites() + + + }); + + $("btnFavs").on("click", function(e) { + e.preventDefault(); + + addToFavorites(); + retrieveFavorites(); + + } ) + + +//----------------------------------------------------------------------- + // Create the Top Stories List + var icon = "" + + function topStories() { + $.get( "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty", function( data ) { + var arrIDs = []; + //create array of Top 20 story IDs, that push a complete url to the array + for (var i = 0; i < 20; i++) { + arrIDs.push("https://hacker-news.firebaseio.com/v0/item/" + data[i] + ".json") + } + //use the url's from above to run another get funtion to get the details for each story ID + for (var i = 0; i < arrIDs.length; i++) { + $.get(arrIDs[i]) + .then(function (data) { + // $('ol').append("
  • " + icon + "
    " + data.title + "
    (" + data.url + ")
  • ") + $('ol').append("
  • " + icon + "
    " + data.title + "
  • ") + + }) + .fail(function(err) { + console.log(err); + }); + } + }) + } + + + + var $email = $('#email').val(); + // console.log($email); + var $password = $('#password').val(); + // console.log($password); + var authKy = ""; + + + function signUpUser(email, password) { + + $.ajax({ + method: "POST", + headers: { + "Content-Type": "application/json" + }, + url: "https://hn-favorites.herokuapp.com/signup", + data: JSON.stringify({ + email: $email, + password: $password + }) + }) + .then(function(data) { authKy = (data.auth_token) }) + .fail(err => console.warn(err)) + + } + + function logInUser(email, password) { + $.ajax({ + method: "POST", + headers: { + "Content-Type": "application/json" + }, + url: "https://hn-favorites.herokuapp.com/signup", + data: JSON.stringify({ + email: $email, + password: $password + }) + }) + .then(function(data) { authKy = (data.auth_token)}) + .fail(err => console.warn(err)) + } + +//If a story is clicked, do addToFavorites() function that will add it to the favorites list + function addToFavorites() { + $.ajax({ + method: "POST", + headers: { + "Content-Type": "application/json", + "Authorization": authKy + }, + url: "https://hn-favorites.herokuapp.com/stories.json", + data: JSON.stringify({ + hacker_news_story: { + by: "aj", + story_id: "123", + title: "Test story", + url: "abc.com" + } + }) + }) + .then(function(data) { console.log(data) }) + .fail(err => console.warn(err)) + } + + function retrieveFavorites() { + $.ajax({ + method: "GET", + headers: { + "Content-Type": "application/json", + "Authorization": authKy + }, + url: "https://hn-favorites.herokuapp.com/stories.json", + data: JSON.stringify({ + hacker_news_story: { + by: "aj", + story_id: "123", + title: "Test story", + url: "abc.com" + } + }) + }) + .then(function(data) { console.log("fav's response: " + data) }) + .fail(err => console.warn(err)) + } + + + +}); + + + + + + + + + + + + + + + + From a8852b88bd2b9a2ec3c314c33742bcb0bf835a90 Mon Sep 17 00:00:00 2001 From: Anthony Jones Date: Mon, 14 Aug 2017 16:41:31 -0700 Subject: [PATCH 10/10] Prototypes Exercises - still working on last problem (bind) --- prototypes_exercise/prototypes.js | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/prototypes_exercise/prototypes.js b/prototypes_exercise/prototypes.js index e69de29b..3fc13697 100644 --- a/prototypes_exercise/prototypes.js +++ b/prototypes_exercise/prototypes.js @@ -0,0 +1,48 @@ +function Person(fName, lName, favColor, favNum, favFoods ) { + this.firstName = fName, + this.lastName = lName, + this.favoriteColor = favColor, + this.favoriteNumber = favNum, + this.favoriteFoods = []; + this.family = []; +} + + +Person.prototype.fullName = function() { + return `${this.firstName} ${this.lastName}`; +}; + + +Person.prototype.toString = function() { + return `${this.fullName()}, Favorite Color: ${this.favoriteColor}, Favorite Number: ${this.favoriteNumber}`; +}; + + +Person.prototype.addToFamily = function(person) { + if(person instanceof Person && !this.family.includes(person)) { + this.family.push(person); + return this.person; + } +}; + +Array.prototype.map = function(cb) { + var newArr = []; + for(let i = 0; i < this.length; i++) { + newArr.push(cb(this[i], i, this)); + } + return newArr; +} + + +String.prototype.reverse = function() { + return this.split("").reverse().join(""); +} + +// still working on figuring out bind + + + + + + +