From 993239d65a699b27bf874f3bb3a08b63bdf6dcf8 Mon Sep 17 00:00:00 2001 From: toChaim Date: Mon, 31 Jul 2017 17:50:26 -0700 Subject: [PATCH 01/24] finished testing --- testing_exercise/testing.js | 41 ++++++++++++++ testing_exercise/testingSpec.js | 96 ++++++++++++++++++++++++++++++++- 2 files changed, 136 insertions(+), 1 deletion(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index e69de29b..7ed5a633 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -0,0 +1,41 @@ +function replaceWith(str, target, replacement){ + return str + .split('') + .map(function(v,i,a){ + if(v === target) return replacement; + else return v; + }).join(''); +} + +function expand(arr, num) { + var a = []; + + for(let i = 0; i < num; i++){ + a = a.concat(arr); + } + + return a; +} + +function acceptNumbersOnly(...args) { + for(let a of args){ + if(typeof a !== 'number' || isNaN(a)){ + return false; + } + } + + return true; +} + +function mergeArrays(arr1, arr2) { + return arr1.concat(arr2).sort(); +} + +function mergeObjects(...args) { + return args.reduce(function(t,v,i,a){ + Object.keys(v).forEach(function(sv, si, sa){ + t[sv] = v[sv]; + }); + return t; + },{}); +} \ No newline at end of file diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index aef56b1d..60ad1315 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -1,3 +1,97 @@ var expect = chai.expect; -// WRITE YOUR TESTS HERE! \ No newline at end of file +describe('This will test the replaceWith function', function(){ + + it('replaceWith should be a function', function() { + expect(typeof replaceWith).to.equal('function'); + }); + it('replaceWith should return correct values', function () { + expect(replaceWith("awesome", "e", "z")).to.equal("awzsomz"); + expect(replaceWith("Foo", "F", "B")).to.equal("Boo"); + }); + it('replaceWith should be case sensitive', function () { + expect(replaceWith("awesomE", "e", "z")).to.equal("awzsomE"); + expect(replaceWith("Foo", "f", "B")).to.equal("Foo"); + expect(replaceWith("ffFFfFB", "f", "B")).to.equal("BBFFBFB"); + }); + +}); + +describe('This will test the expand function', function(){ + + it('expand should be a function', function() { + expect(typeof expand).to.equal('function'); + }); + it('expand should return correct values', 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"]); + expect(expand(["foo", "test"],0)).to.deep.equal([]); + }); + +}); + +describe('This will test the acceptNumbersOnly function', function(){ + + it('expand should be a function', function() { + expect(typeof acceptNumbersOnly).to.equal('function'); + }); + + it('acceptNumbersOnly should return correct values', function () { + expect(acceptNumbersOnly(1,"foo")).to.equal(false); + expect(acceptNumbersOnly(1,2,3,4,5,6,7)).to.equal(true); + expect(acceptNumbersOnly(1)).to.equal(true); + }); + + it('acceptNumbersOnly should deal with objects input', function () { + expect(acceptNumbersOnly([])).to.equal(false); + expect(acceptNumbersOnly({})).to.equal(false); + }); + + it('acceptNumbersOnly should deal with strings input', function () { + expect(acceptNumbersOnly('')).to.equal(false); + expect(acceptNumbersOnly('5')).to.equal(false); + }); + + it('acceptNumbersOnly should deal with null input', function () { + expect(acceptNumbersOnly(null)).to.equal(false); + }); + + it('acceptNumbersOnly should deal with NaN input', function () { + expect(acceptNumbersOnly(NaN)).to.equal(false); + }); +}); + +describe('This will test the mergeArrays function', function(){ + + it('mergeArrays should be a function', function() { + expect(typeof mergeArrays).to.equal('function'); + }); + it('mergeArrays should return correct values', function () { + expect(mergeArrays([2,1],[3,4])).to.deep.equal([1,2,3,4]); + }); + +}); + +describe('This will test the mergeObjects function', function(){ + var obj1= { + name: "Foo", + num: 33 + } + var obj2 = { + test: "thing", + num: 55 + } + var obj12 = { + name: "Foo", + test: "thing", + num: 55 + } + + it('mergeObjects should be a function', function() { + expect(typeof mergeObjects).to.equal('function'); + }); + it('mergeObjects should return correct values', function () { + expect(mergeObjects(obj1, obj2)).to.deep.equal(obj12); + }); + +}); \ No newline at end of file From 31b4be41373439202855f7a1c689fbd4f15ebde5 Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 2 Aug 2017 18:07:39 -0700 Subject: [PATCH 02/24] fix some tests and my progress so far. --- lodash_exercise/lodash.js | 120 ++++++++++++++++++++++++++-------- lodash_exercise/lodashSpec.js | 19 +++++- 2 files changed, 110 insertions(+), 29 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 483d734a..cbd45685 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,41 +1,109 @@ -function drop(){ - -} - -function fromPairs(){ - -} - -function head(){ - -} - -function take(){ - +function drop(arr, skip = 1){ + return arr.slice(skip); } -function takeRight(){ +function fromPairs(arr){ + var obj = {}; -} - -function union(){ + for(let p = 0; p < arr.length; p++){ + obj[arr[p][0]] = arr[p][1]; + } + return obj; } -function zipObject(){ - +function head(arr){ + return arr[0]; } -function includes(){ - +function take(arr, n = 1){ + return arr.slice(0, n); } -function sample(){ - +function takeRight(arr, n = 1){ + return arr.reduce(function(t,c,i,a){ + if(i > arr.length-1-n){ + t.push(c); + return t; + }else return t; + },[]); } -function cloneDeep(){ - +function union(){ + var resObj = {}; + var resArr = []; + + for(let i = 0; i < arguments.length; i++){ + let arr = arguments[i]; + + for(let v of arr){ + var newKey = !resObj.hasOwnProperty(JSON.stringify(v)); + if(newKey){ + resArr.push(v); + resObj[JSON.stringify(v)] = 'Now I have it.'; + } + } + + } + + return resArr; +} + +function zipObject(keys, vals){ + var obj = {}; + + for(let i = 0; i < keys.length; i++){ + obj[keys[i]] = vals[i]; + } + + return obj; +} + +function includes(input, val, fromIndex = 0){ + if(Array.isArray(input)){ + for(let i = fromIndex; i < input.length; i++) + { + if(input[i] === val) + return true; + } + }else if(typeof input === 'object'){ + for(let k in input){ + if(input[k] === val){ + return true; + } + } + }else if(typeof input === 'string'){ + for(let i = fromIndex; i < input.length; i++){ + var temp = input.slice(i, i+ val.length); + if(temp === val) return true; + } + } + + return false; +} + +function sample(obj){ + if(Array.isArray(obj)){ + return obj[Math.floor(Math.random() * obj.length)]; + } + else{ + let keys = Object.keys(obj); + let k = Math.floor(Math.random() * keys.length); + return(obj[keks[k]]); + } + +} + +function cloneDeep(obj){ + var newObj = {} + if(Array.isArray(obj)) newObj = []; + + for(let k in obj){ + if(typeof obj[k] === 'object'){ newObj[k] = cloneDeep(obj[k]);} + else newObj[k] = obj[k]; + } + + return newObj; } function sumBy(){ diff --git a/lodash_exercise/lodashSpec.js b/lodash_exercise/lodashSpec.js index f0e90e9c..8f0d1707 100644 --- a/lodash_exercise/lodashSpec.js +++ b/lodash_exercise/lodashSpec.js @@ -73,6 +73,15 @@ describe("#takeRight", function(){ describe("#union", function(){ it("returns a new array of combined values.", function(){ expect(union([2], [1, 2])).to.deep.equal([2, 1]) + expect(union([2])).to.deep.equal([2]) + }); + it("takes arrays of other primetives", function(){ + expect(union([2], ['one', 'two'])).to.deep.equal([2, 'one', 'two']) + expect(union(['one', 'two', 'three'], ['three', 'four'])).to.deep.equal(['one', 'two', 'three', 'four']) + }); + it("takes arrays of objects", function(){ + expect(union([[2]], ['one', [2], 'two', [2,3],{'number':4}])).to.deep.equal([[2], 'one', 'two', [2,3], {'number':4}]) + expect(union(['one', 'two', 'three'], ['three', 'four'])).to.deep.equal(['one', 'two', 'three', 'four']) }); }); @@ -110,18 +119,22 @@ describe("#cloneDeep", function(){ it("should create a copy of an array when passed one", function(){ expect(cloneDeep([1,2,3])).to.be.instanceof(Array); }); - it("should create a copy of an array when passed one", function(){ + it("should create a copy of an object when passed one", function(){ expect(cloneDeep({})).to.be.instanceof(Object); }); it("should create a deep copy of an array", function(){ var objects = [{ 'a': 1 }, { 'b': 2 }]; var deep = cloneDeep(objects); expect(deep[0] === objects[0]).to.equal(false) + expect(deep).to.deep.equal(objects) }); it("should create a deep copy of an object", function(){ - o = {} - o2 = cloneDeep(o) + var o = {} + var o2 = cloneDeep(o) expect(o === o2).to.equal(false) + o = {'k': 1, 'k2': [1,2,3,{'val': true, 'val1': 'string'},4,5,6], 'k3': 'finish'}; + expect(cloneDeep(o)===o).to.equal(false) + expect(cloneDeep(o)).to.deep.equal(o) }); }); From 4fc875be7315946aa705e80f50fca820fa4c4669 Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 2 Aug 2017 18:12:34 -0700 Subject: [PATCH 03/24] a little more done. --- lodash_exercise/lodash.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index cbd45685..7e77d5ca 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -106,7 +106,7 @@ function cloneDeep(obj){ return newObj; } -function sumBy(){ +function sumBy(array, fn = function()){ } From f40174654bc7f37859c66178772798e1d2b540ac Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 2 Aug 2017 18:24:43 -0700 Subject: [PATCH 04/24] fix sort on mergeArrays and remove unesercery arguments from callback functions. --- testing_exercise/testing.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index 7ed5a633..2f3d050e 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -1,7 +1,7 @@ function replaceWith(str, target, replacement){ return str .split('') - .map(function(v,i,a){ + .map(function(v){ if(v === target) return replacement; else return v; }).join(''); @@ -28,12 +28,12 @@ function acceptNumbersOnly(...args) { } function mergeArrays(arr1, arr2) { - return arr1.concat(arr2).sort(); + return arr1.concat(arr2).sort(function(a, b){return a-b;}); } function mergeObjects(...args) { - return args.reduce(function(t,v,i,a){ - Object.keys(v).forEach(function(sv, si, sa){ + return args.reduce(function(t,v){ + Object.keys(v).forEach(function(sv){ t[sv] = v[sv]; }); return t; From 551ac19bdae941b6d22b3b2030457402a3e3f289 Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 2 Aug 2017 18:54:00 -0700 Subject: [PATCH 05/24] inRange done --- lodash_exercise/lodash.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 7e77d5ca..07abc8d3 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -106,12 +106,35 @@ function cloneDeep(obj){ return newObj; } -function sumBy(array, fn = function()){ +function sumBy(arr, cb){ + var total = 0; + var fn; + if(typeof cb === 'string'){ + fn = function(obj){ + return obj[cb]; + } + }else{ + fn = cb; + } -} + for(let e of arr){ + total += fn(e); + } -function inRange(){ + return total; +} +function inRange(num, start, end){ + if(!end){ + end = start; + start = 0; + } + if(end < start){ + let temp = end; + end = start; + start = temp; + } + return num >= start && num < end; } function has(){ From de0972b9162c2a444298f8bf3821cf960209ee10 Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 2 Aug 2017 18:56:54 -0700 Subject: [PATCH 06/24] refactor and improve inRange. Now it the non inclusive end is right for up and down. --- lodash_exercise/lodash.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 07abc8d3..0204f926 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -129,12 +129,14 @@ function inRange(num, start, end){ end = start; start = 0; } + if(end < start){ - let temp = end; - end = start; - start = temp; + return num <= start && num > end; } - return num >= start && num < end; + else { + return num >= start && num < end; + } + } function has(){ From 8166895920c7e9a59c17000b806f35ecb991b2cf Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 2 Aug 2017 23:33:00 -0700 Subject: [PATCH 07/24] finish lodash --- lodash_exercise/lodash.js | 131 ++++++++++++++++++++++++++++++++++---- 1 file changed, 120 insertions(+), 11 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 0204f926..18cbde7e 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -139,54 +139,163 @@ function inRange(num, start, end){ } -function has(){ +function has(obj, path){ + var keys, isDefined = obj; + if(typeof path === 'string') { keys = path.split('.'); } + else { keys = path; } + + for(let k of keys){ + isDefined = isDefined[k]; + if(isDefined === undefined) return false; + } + + return true; + + /* + var keys; + if(typeof path === 'string') { keys = path.split('.'); } + else { keys = path; } + + if(keys.length === 0) return true; + if(obj[keys[0]] === undefined) return false; + return has(obj[keys], keys.slice[1]); + */ } -function omit(){ +function omit(obj, keys){ + var newObj = {}; + + for(let key in obj){ + newObj[key] = obj[key]; + } + + for(let key of keys){ + delete(newObj[key]); + } + return newObj; } -function pick(){ +function pick(obj, keys){ + var newObj = {}; + for(let k of keys){ + newObj[k] = obj[k]; + } + + return newObj; } -function pickBy(){ +function pickBy(obj, fn){ + var newObj = {}; + for(let key in obj){ + if(fn(obj[key])) newObj[key] = obj[key]; + } + + return newObj; } -function omitBy(){ +function omitBy(obj, fn){ + var newObj = {}; + for(let key in obj){ + if(!fn(obj[key])) newObj[key] = obj[key]; + } + + return newObj; } -function padEnd(){ +function padEnd(str, len, pad = " "){ + var newStr = str; + + while(newStr.length < len) { newStr += pad; } + return newStr.substring(0, len); } -function repeat(){ +function repeat(str, num){ + var newStr = ''; + for(let i = 0; i < num; i++) newStr += str; + + return newStr; } function upperFirst(str){ + return str[0].toUpperCase() + str.substring(1); + } -function flatten(){ +function flatten(arr){ + var newArr = []; + + for(let v of arr){ + if(Array.isArray(v)){ + for(let sv of v) newArr.push(sv); + } + else { + newArr.push(v); + } + } + return newArr; } function zip(){ + var newArr = []; + + for(let a = 0; a < arguments[0].length; a++){ + let temp = []; + + for(let i = 0; i < arguments.length; i++){ + temp.push(arguments[i][a]); + } + + newArr.push(temp); + } + return newArr; } -function unzip(){ +function unzip(arr){ + var newArr = []; + for(let a = 0; a < arr[0].length; a++){ + let temp = []; + + for(let i = 0; i < arr.length; i++){ + temp.push(arr[i][a]); + } + + newArr.push(temp); + } + + console.log(newArr); + return newArr; } -function flip(){ +function flip(fn){ + return function(...args){ + var rev = args.reverse(); + return fn(...rev); + } } -function flattenDeep(){ +function flattenDeep(arr){ + var newArr = []; + + for(let v of arr){ + if(Array.isArray(v)){ + newArr = newArr.concat(flattenDeep(v)); + } + else { + newArr.push(v); + } + } + return newArr; } From 14ac4959837e7ee00573bb8cca1c8de9b200df78 Mon Sep 17 00:00:00 2001 From: toChaim Date: Thu, 3 Aug 2017 17:08:48 -0700 Subject: [PATCH 08/24] game working, responsive working. --- canvas_exercise/shapes_game/index.html | 64 ++++++++++----- canvas_exercise/shapes_game/index.js | 105 ++++++++++++++++++++++--- canvas_exercise/shapes_game/styles.css | 13 ++- 3 files changed, 149 insertions(+), 33 deletions(-) diff --git a/canvas_exercise/shapes_game/index.html b/canvas_exercise/shapes_game/index.html index 97529df5..6232fca5 100644 --- a/canvas_exercise/shapes_game/index.html +++ b/canvas_exercise/shapes_game/index.html @@ -10,33 +10,57 @@
-
- -
-
-

Score

-

0

-

Timer

-

30

-
-

Legend

-
-
-
-

Left

-
-
-

Right

+ +
+
+ +
+ +
+ +
+

Score

+

0

+
+
+

Timer

+

30

+
+ +
+ +

Legend

+ +
+
+
+

Left

+
+ +
+
+
+

Right

+
+ +

Up

-
-
+
+ +
+

Down

-
+ +
+ +
+
+
\ No newline at end of file diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 0de5f18a..05b5f133 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -1,15 +1,91 @@ window.addEventListener("load", function() { - function clear(ctx, width, heigt) { + function clear() { + ctx.clearRect(0,0,width,height); } - function drawRandomShape(ctx, width, height) { + function drawRandomShape() { + let top = Math.floor(Math.random() * (height -50)); + let left = Math.floor(Math.random() * (width -50)); + let pick = Math.floor(Math.random() * 4); + clear(); + + switch (pick){ + case 0: //white0 white triangle = up + expectedKey = 'ArrowUp'; + ctx.fillStyle = "white"; + ctx.beginPath(); + ctx.moveTo(left,top); + ctx.lineTo(left+50, top+50); + ctx.lineTo(left, top+50); + ctx.fill(); + ctx.closePath(); + break; + case 1: //red1 red square = down + expectedKey = 'ArrowDown'; + ctx.fillStyle = 'red'; + ctx.fillRect( + top, + left, + 50, + 50); + break; + case 2: //red0 red triangle = left + expectedKey = 'ArrowLeft'; + ctx.fillStyle = "red"; + ctx.beginPath(); + ctx.moveTo(left,top); + ctx.lineTo(left+50, top+50); + ctx.lineTo(left, top+50); + ctx.fill(); + ctx.closePath(); + break; + case 3: //white1 white square = right + expectedKey = 'ArrowRight'; + ctx.fillStyle = 'white'; + ctx.fillRect( + top, + left, + 50, + 50); + break; + } + } - function drawGameStartText(ctx, width, height, score) { + function drawGameStartText() { + console.log('End Game'); + clear(); + ctx.font = '2em serif'; + ctx.textAlign = 'center'; + ctx.fillStyle = 'white'; + ctx.fillText('Press the space bar to start the game.', + Math.floor(width/2), + Math.floor(height/2.25)); + if(score > 0){ + ctx.fillText('Last score is ' + score, + Math.floor(width/2), + Math.floor(height/2)); + } + } - function restartGame(ctx, width, height) { + function startGame(){ + console.log('Starting Game'); + scoreSpan.innerText = score = 0; + seconds = 30; + gameOn = true; + clear(); + drawRandomShape(); + + intervalId = setInterval(function(){ + timerSpan.innerText = --seconds; + if(seconds <= 0) { + clearInterval(intervalId); + gameOn = false; + drawGameStartText(); + } + }, 1000); } var canvas = document.getElementById("shapes-game"), @@ -18,19 +94,28 @@ window.addEventListener("load", function() { gameOn = false, expectedKey = undefined, ctx = canvas.getContext('2d'), - // white triangle = up, red square = down, - // red triangle = left, white square = 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 = 0, intervalId; canvas.width = width; canvas.height = height; - document.addEventListener("keyup", function() { - + document.addEventListener("keyup", function(e) { + if(e.key === ' ' && !gameOn){ + startGame(); + }else if(gameOn && e.key == expectedKey){ //right key + scoreSpan.innerText = ++score; + drawRandomShape(); + }else if(gameOn && 'ArrowUpArrowDownArrowLeftArrowRight'.includes(e.key)){ //wrong key + scoreSpan.innerText = --score; + drawRandomShape(); + } }); + + //On Load + drawGameStartText(); }); diff --git a/canvas_exercise/shapes_game/styles.css b/canvas_exercise/shapes_game/styles.css index cbe4fa83..84caaed0 100644 --- a/canvas_exercise/shapes_game/styles.css +++ b/canvas_exercise/shapes_game/styles.css @@ -11,11 +11,11 @@ body { padding: 10px; } .canvas-container, #shapes-game { - height: 750px; - width: 800px; + height: 90vh; } -.scores { +.key { + height: 90vh; padding: 10px; text-align: center; } @@ -30,6 +30,7 @@ body { } .triangle-bottomleft-red { + margin: 0 auto; width: 0; height: 0; border-bottom: 50px solid red; @@ -37,6 +38,7 @@ body { } .triangle-bottomleft-black { + margin: 0 auto; width: 0; height: 0; border-bottom: 54px solid black; @@ -44,6 +46,7 @@ body { } .triangle-inner-white { + margin: 0 auto; position: relative; top: 2px; left: 2px; @@ -54,6 +57,7 @@ body { } .triangle-left { + margin: 0 auto; width: 0; height: 0; border-top: 23px solid transparent; @@ -62,6 +66,7 @@ body { } .inner-triangle { + margin: 0 auto; position: relative; top: -20px; left: 2px; @@ -73,12 +78,14 @@ body { } .red-square { + margin: 0 auto; width: 50px; height: 50px; background-color: red; } .white-square { + margin: 0 auto; width: 50px; height: 50px; background-color: white; From 32904c3cbc80fb3698ca0cebcd80641aebf0eb87 Mon Sep 17 00:00:00 2001 From: toChaim Date: Fri, 4 Aug 2017 20:30:19 -0700 Subject: [PATCH 09/24] 2015 exersiexercises --- es2015_exercise/readme.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/es2015_exercise/readme.md b/es2015_exercise/readme.md index 9e482efa..8abdd80d 100644 --- a/es2015_exercise/readme.md +++ b/es2015_exercise/readme.md @@ -11,16 +11,31 @@ var person = { }.bind(this),1000) } } + +// 2015 +var person = { + fullName: "Harry Potter", + sayHi(){ + setTimeout(() => console.log(`Your name is ${this.fullName}`),1000) + } +} ``` ```javascript var name = "Josie" console.log("When " + name + " comes home, so good") + +// 2015 +var name = "Josie"; +console.log(`When ${name} comes home, so good`); ``` ```javascript var DO_NOT_CHANGE = 42; DO_NOT_CHANGE = 50; // stop me from doing this! + +// 2015 +const DO_NOT_CHANGE = 42; ``` ```javascript @@ -28,6 +43,10 @@ var arr = [1,2] var temp = arr[0] arr[0] = arr[1] arr[1] = temp + +// 2015 +let [ta, tb, ...tc] = arr; +arr = [tb, ta, ...tc]; ``` ```javascript @@ -36,6 +55,9 @@ function double(arr){ return val*2 }); } + +// 2015 +var double = arr => arr.map(val => val*2); ``` ```javascript @@ -48,6 +70,9 @@ var obj = { var a = obj.numbers.a; var b = obj.numbers.b; + +// 2015 +var {a, b} = obj.numbers; ``` ```javascript @@ -62,6 +87,9 @@ function add(a,b){ } return a+b } + +// 2015 +var add = (a = 10,b = 10) => a+b; ``` Research the following functions - what do they do? From ba3afd81c05ed681b55b9c1310c460046631754a Mon Sep 17 00:00:00 2001 From: toChaim Date: Mon, 7 Aug 2017 16:47:50 -0700 Subject: [PATCH 10/24] half finished. --- canvas_exercise/shapes_game/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 05b5f133..a42dbc54 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -25,8 +25,8 @@ window.addEventListener("load", function() { expectedKey = 'ArrowDown'; ctx.fillStyle = 'red'; ctx.fillRect( - top, left, + top, 50, 50); break; From 633ce47621ca1ca32abd19195ec9db63b537db85 Mon Sep 17 00:00:00 2001 From: toChaim Date: Mon, 7 Aug 2017 22:59:02 -0700 Subject: [PATCH 11/24] functioning is fine. styleing needs work. --- .DS_Store | Bin 0 -> 8196 bytes jquery_exercise/icon.gif | Bin 0 -> 62 bytes jquery_exercise/icon.png | 0 jquery_exercise/index.css | 0 jquery_exercise/index.html | 44 +++++++++++++++++++++++++ jquery_exercise/index.js | 64 +++++++++++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+) create mode 100644 .DS_Store create mode 100644 jquery_exercise/icon.gif create mode 100644 jquery_exercise/icon.png create mode 100644 jquery_exercise/index.css create mode 100644 jquery_exercise/index.html create mode 100644 jquery_exercise/index.js diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1034b25b036bd0fe6800b2f6e808a91c8dac90af GIT binary patch literal 8196 zcmeHM!EVz)5S?v8NrX@$Kx*NDd_m$6P(oXRgpi_84!t1-!2wWe$0l{<*ui!hN(jn@ ze?WpC;9K|rK7GHFkl!siwxkM%~Cqyy|1pC+Av@k_%9jY z=Yxg9x~26@Y5C|tB~t+CBD$rZ&FKJREUjBw&y>a$h)_^y1r<8Q5DJcdOOI=5JyTl2 zN$BK5=$VDiP=uTv_ANP_L`!K?8wLymM;YL7xk@(eqrA!F@B8$cM&waUhv@fkmD@e! z_X&ABLDX}C$N`!y+()_jLke2_IiUx80gf*85Jw=bI6#Nw=GyzRWUnIag>(RGNgi6P zEupUi9bw)CUnXMQQx7^qt4ptN7Rqy9kI1=G@nOk!1*{}=hw9Xzt=yG;UqY{fj{)wf z&sL7%^y31jRmoi{cVU3q#kq|;@i1$IW4MH84j1ti?C~G(sO0c3(9Sp+YJd3Z3-BG9rBFP#dcIVKTr9ur>_y{FYNw;5ZO6~$neTdzZa;~lVBWT!r@lAtR<$j!PTo!5e*#4iq2DP~QRnmeGg>*c9dzO_h{8UbK`Fq|eR8r}A)O{mW`GTI zr8D!Vvfl=mV5eC`Uq(2^z_Syl2&*CRxk14pDoP9PRE7-?D{$d}9^FRmW>-ycpbX-Y zMD!3cG3HYNlecz+V#Y>QIxGhwKM4A{_l|JO;OBc~-v96lBZh%fFi=p~b + + + + Linker + + + + + +
+
+
+
icon
+ +
+ +
+ +
+
+ + + + + \ No newline at end of file diff --git a/jquery_exercise/index.js b/jquery_exercise/index.js new file mode 100644 index 00000000..cebc63e7 --- /dev/null +++ b/jquery_exercise/index.js @@ -0,0 +1,64 @@ +$(document).ready(function(){ + var $submit_form = $('#submit-form'); + var $inp_title = $('#inp-title'); + var $inp_url = $('#inp-url'); + var links = JSON.parse(localStorage.getItem('links')); + if(links === null){ + links = [ + { + favor: true, + title: "Isn't this the best site you have ever seen?", + link: 'file:///Users/chaim/Dropbox/rithm/canvas.html' + }]; + } + var $links = $('#links'); + + function displayLink(favor, title, link){ + let html = ''; + let shortLink = link.split('/').filter(v => v.includes('.')); + if(favor) html = ''; + html += '' + title + '(' + shortLink[0] + ')'; + let $lin = $('
  • '); + $lin.html(html); + if(favor) $lin.addClass('favor'); + $links.append($lin); + } + + function saveLinks() { + localStorage.setItem('links', JSON.stringify(links)); + } + + for(let lin of links){ + displayLink(lin.favor, lin.title, lin.link); + } + + $('#submit-link').click(function(){ + $submit_form.toggleClass("hidden"); + }); + $('#favor-link').click(function(){ + $links.find('li').not('.favor').toggleClass('hidden'); + }); + $submit_form.submit(function(e){ + e.preventDefault(); + + displayLink(false, $inp_title.val(), $inp_url.val()); + links.push({ + favor: false, + title: $inp_title.val(), + link: $inp_url.val()}); + + saveLinks(); + + }); + $links.on('click', '.star', function(e){ + e.preventDefault(); + + var $target = $(e.target); + + $target.toggleClass('glyphicon-star-empty glyphicon-star'); + var index = $target.parent().index(); + links[index].favor = $target.hasClass('glyphicon-star'); + saveLinks(); + }); + +}); \ No newline at end of file From 839272ffb888635a8d4c7db5eb73b91acfaaddf4 Mon Sep 17 00:00:00 2001 From: toChaim Date: Tue, 8 Aug 2017 09:11:25 -0700 Subject: [PATCH 12/24] minore improvments. --- jquery_exercise/index.css | 3 +++ jquery_exercise/index.html | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/jquery_exercise/index.css b/jquery_exercise/index.css index e69de29b..91dcd74f 100644 --- a/jquery_exercise/index.css +++ b/jquery_exercise/index.css @@ -0,0 +1,3 @@ +.row{ + margin-bottom: 30px; +} \ No newline at end of file diff --git a/jquery_exercise/index.html b/jquery_exercise/index.html index 57dceffc..40465cb4 100644 --- a/jquery_exercise/index.html +++ b/jquery_exercise/index.html @@ -9,7 +9,7 @@
    -
    +
    icon
      From b65ff9dabee5c7d8343cad81e191b3e619241194 Mon Sep 17 00:00:00 2001 From: toChaim Date: Tue, 8 Aug 2017 10:23:41 -0700 Subject: [PATCH 13/24] fix favoriets filter bug --- jquery_exercise/index.css | 3 +++ jquery_exercise/index.js | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/jquery_exercise/index.css b/jquery_exercise/index.css index 91dcd74f..345814a7 100644 --- a/jquery_exercise/index.css +++ b/jquery_exercise/index.css @@ -1,3 +1,6 @@ .row{ margin-bottom: 30px; +} +.glyphicon{ + margin: 5px; } \ No newline at end of file diff --git a/jquery_exercise/index.js b/jquery_exercise/index.js index cebc63e7..99acf9da 100644 --- a/jquery_exercise/index.js +++ b/jquery_exercise/index.js @@ -46,7 +46,8 @@ $(document).ready(function(){ favor: false, title: $inp_title.val(), link: $inp_url.val()}); - + $inp_title.val(''); + $inp_url.val(''); saveLinks(); }); @@ -58,6 +59,7 @@ $(document).ready(function(){ $target.toggleClass('glyphicon-star-empty glyphicon-star'); var index = $target.parent().index(); links[index].favor = $target.hasClass('glyphicon-star'); + $target.parent().toggleClass('favor'); saveLinks(); }); From 9db7da1581eec174dc6bbcac2a485eb204961250 Mon Sep 17 00:00:00 2001 From: toChaim Date: Tue, 8 Aug 2017 17:10:40 -0700 Subject: [PATCH 14/24] first commit on new progject --- jquery_exercise/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/jquery_exercise/index.js b/jquery_exercise/index.js index 99acf9da..630bd825 100644 --- a/jquery_exercise/index.js +++ b/jquery_exercise/index.js @@ -21,6 +21,7 @@ $(document).ready(function(){ let $lin = $('
    • '); $lin.html(html); if(favor) $lin.addClass('favor'); + $lin.addClass('bg-warning') $links.append($lin); } From acf47261e79813b8bc9700172a2fc6597a41aaff Mon Sep 17 00:00:00 2001 From: toChaim Date: Tue, 8 Aug 2017 23:59:35 -0700 Subject: [PATCH 15/24] working on using login --- ajax_with_jquery_exercise/index.css | 9 +++ ajax_with_jquery_exercise/index.html | 45 ++++++++++++++ ajax_with_jquery_exercise/index.js | 89 ++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 ajax_with_jquery_exercise/index.css create mode 100644 ajax_with_jquery_exercise/index.html create mode 100644 ajax_with_jquery_exercise/index.js diff --git a/ajax_with_jquery_exercise/index.css b/ajax_with_jquery_exercise/index.css new file mode 100644 index 00000000..01029487 --- /dev/null +++ b/ajax_with_jquery_exercise/index.css @@ -0,0 +1,9 @@ +.row{ + margin-bottom: 30px; +} +.glyphicon{ + margin: 5px; +} +.hideNoLogin{ + visibility: hidden; +} \ No newline at end of file diff --git a/ajax_with_jquery_exercise/index.html b/ajax_with_jquery_exercise/index.html new file mode 100644 index 00000000..f4110732 --- /dev/null +++ b/ajax_with_jquery_exercise/index.html @@ -0,0 +1,45 @@ + + + + + Linker + + + + + +
      +
      +
      icon
      +
      +
      +
      + +
      + +
      +
      + + + + + \ No newline at end of file diff --git a/ajax_with_jquery_exercise/index.js b/ajax_with_jquery_exercise/index.js new file mode 100644 index 00000000..2f31e981 --- /dev/null +++ b/ajax_with_jquery_exercise/index.js @@ -0,0 +1,89 @@ +$(document).ready(function(){ + var $links = $('#links'); + var $login_form = $('#login-form'); + var $inp_email = $('#inp-email'); + var $inp_password = $('#inp-password'); + var authorization = ''; + + $('#login-link').click(() => { $login_form .toggleClass("hidden") }); + $('#login-btn').click(function(e){ + e.preventDefault(); + //var email = "lane.matthew@gmail.com"; + var email = $inp_email.val(); + //var password = "password"; + var password = $inp_password.val(); + + $.ajax({ + method: "POST", + headers: { + "Content-Type": "application/json" + }, + url: "https://hn-favorites.herokuapp.com/login", + data: JSON.stringify({ + 'email': email, + 'password': password + }) + }) + .then(function(data) { + localStorage.setItem('auth_token', data.auth_token); + authorization = data.auth_token; + console.log(data.auth_token); + $('.hideNoLogin').toggleClass('hideNoLogin showOnLogin'); + //getFavorites(data.auth_token); + }) + .fail(err => console.warn(err)); + }); + + getStories(); + + function getStories(login = '', favorites = false){ + var url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'; + console.log('login is ' + login); + + $.get(url) + .then(function(res){ + for(let i = 0; i < 20; i++){ + var newUrl = `https://hacker-news.firebaseio.com/v0/item/${res[i]}.json?print=pretty`; + $.get(newUrl).then(function(res){ + displayLink(res.title, res.url) + }); + } + + }) + .fail(function(err){ + console.warn(err); + }); + } + + function getFavorites(login) { + var url = 'https://hacker-news.firebaseio.com/favorites.herokuapp.com/stories.json'; + + $.ajax({ + method: "POST", + headers: { + "Content-Type": "application/json", + "Authorization": authorization, + }, + 'url': url, + }) + .then(function(data) { + console.log(data); + }) + .fail(err => console.warn(err)); + + } + + function displayLink(title, link, favor = false){ + let html = ''; + let shortLink = ''; + if(link) { shortLink = link.split('/').filter(v => v.includes('.')); } + if(favor) html = ''; + { html += `${title}(${shortLink[0]})`; } + let $lin = $('
    • '); + $lin.html(html); + if(favor) $lin.addClass('favor'); + $lin.addClass('bg-warning') + $links.append($lin); + } + +}); \ No newline at end of file From fe30029872a311a73298b624f91b057c213281f5 Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 9 Aug 2017 08:48:25 -0700 Subject: [PATCH 16/24] got get of favoriets array working. improve login click. --- ajax_with_jquery_exercise/index.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/ajax_with_jquery_exercise/index.js b/ajax_with_jquery_exercise/index.js index 2f31e981..0c5dda92 100644 --- a/ajax_with_jquery_exercise/index.js +++ b/ajax_with_jquery_exercise/index.js @@ -1,11 +1,12 @@ $(document).ready(function(){ var $links = $('#links'); + var $login_link = $('#login-link'); var $login_form = $('#login-form'); var $inp_email = $('#inp-email'); var $inp_password = $('#inp-password'); var authorization = ''; - $('#login-link').click(() => { $login_form .toggleClass("hidden") }); + $('#login-link').click(() => { $login_form .toggleClass("hidden"); }); $('#login-btn').click(function(e){ e.preventDefault(); //var email = "lane.matthew@gmail.com"; @@ -29,22 +30,32 @@ $(document).ready(function(){ authorization = data.auth_token; console.log(data.auth_token); $('.hideNoLogin').toggleClass('hideNoLogin showOnLogin'); - //getFavorites(data.auth_token); + $login_link.text('Log Out'); + $inp_email.val(''); + $inp_password.val(''); + $login_form .toggleClass("hidden"); + return $.ajax( + { + method: "GET", + headers: { "Authorization": authorization }, + 'url': url, + }); }) + .then() .fail(err => console.warn(err)); }); getStories(); - function getStories(login = '', favorites = false){ + function getStories(login = '', favorites = []){ var url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'; - console.log('login is ' + login); $.get(url) .then(function(res){ for(let i = 0; i < 20; i++){ var newUrl = `https://hacker-news.firebaseio.com/v0/item/${res[i]}.json?print=pretty`; $.get(newUrl).then(function(res){ + if(favorites) displayLink(res.title, res.url) }); } @@ -56,18 +67,16 @@ $(document).ready(function(){ } function getFavorites(login) { - var url = 'https://hacker-news.firebaseio.com/favorites.herokuapp.com/stories.json'; + var url = 'https://hn-favorites.herokuapp.com/stories.json'; $.ajax({ - method: "POST", + method: "GET", headers: { - "Content-Type": "application/json", "Authorization": authorization, }, 'url': url, }) .then(function(data) { - console.log(data); }) .fail(err => console.warn(err)); @@ -78,7 +87,7 @@ $(document).ready(function(){ let shortLink = ''; if(link) { shortLink = link.split('/').filter(v => v.includes('.')); } if(favor) html = ''; - { html += `${title}(${shortLink[0]})`; } + { html += `${title} ( ${shortLink[0]} )`; } let $lin = $('
    • '); $lin.html(html); if(favor) $lin.addClass('favor'); From 380ef7678d8f9caa2581a8f5ebe5138c75525ef6 Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 9 Aug 2017 16:14:59 -0700 Subject: [PATCH 17/24] start on callApplyBind problems --- ajax_with_jquery_exercise/index.html | 5 ++-- ajax_with_jquery_exercise/index.js | 23 ++++++++++------- call_apply_bind_exercise/callApplyBind.js | 28 ++++++++++++++++++++ call_apply_bind_exercise/readme.md | 31 +++++++++++++++++++++++ 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/ajax_with_jquery_exercise/index.html b/ajax_with_jquery_exercise/index.html index f4110732..b918d3f6 100644 --- a/ajax_with_jquery_exercise/index.html +++ b/ajax_with_jquery_exercise/index.html @@ -22,11 +22,11 @@
      - +
      - +
      @@ -35,6 +35,7 @@
    diff --git a/ajax_with_jquery_exercise/index.js b/ajax_with_jquery_exercise/index.js index 0c5dda92..e6d55841 100644 --- a/ajax_with_jquery_exercise/index.js +++ b/ajax_with_jquery_exercise/index.js @@ -1,5 +1,7 @@ $(document).ready(function(){ var $links = $('#links'); + var linksList = []; + var favList = []; var $login_link = $('#login-link'); var $login_form = $('#login-form'); var $inp_email = $('#inp-email'); @@ -29,7 +31,6 @@ $(document).ready(function(){ localStorage.setItem('auth_token', data.auth_token); authorization = data.auth_token; console.log(data.auth_token); - $('.hideNoLogin').toggleClass('hideNoLogin showOnLogin'); $login_link.text('Log Out'); $inp_email.val(''); $inp_password.val(''); @@ -38,25 +39,27 @@ $(document).ready(function(){ { method: "GET", headers: { "Authorization": authorization }, - 'url': url, + 'url': 'https://hn-favorites.herokuapp.com/stories.json', }); }) - .then() + .then(function(data){ + getStories(true, data); + }) .fail(err => console.warn(err)); }); getStories(); - function getStories(login = '', favorites = []){ + function getStories(login = false, favorites = []){ var url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'; $.get(url) .then(function(res){ + $links.empty(); for(let i = 0; i < 20; i++){ var newUrl = `https://hacker-news.firebaseio.com/v0/item/${res[i]}.json?print=pretty`; $.get(newUrl).then(function(res){ - if(favorites) - displayLink(res.title, res.url) + displayLink(res.title, res.url, res.id, false, login); }); } @@ -82,11 +85,13 @@ $(document).ready(function(){ } - function displayLink(title, link, favor = false){ - let html = ''; + function displayLink(title, link, storyId=0, favor = false, login = false){ + var loginClass = 'hideNoLogin'; + if(login) loginClass = 'showOnLogin'; + let html = ``; let shortLink = ''; if(link) { shortLink = link.split('/').filter(v => v.includes('.')); } - if(favor) html = ''; + if(favor) html = ``; { html += `${title} ( ${shortLink[0]} )`; } let $lin = $('
  • '); $lin.html(html); diff --git a/call_apply_bind_exercise/callApplyBind.js b/call_apply_bind_exercise/callApplyBind.js index e69de29b..ef7ecca0 100644 --- a/call_apply_bind_exercise/callApplyBind.js +++ b/call_apply_bind_exercise/callApplyBind.js @@ -0,0 +1,28 @@ +var obj = { + fullName: "Harry Potter", + person: { + sayHi: function(){ + return "This person's name is " + obj.fullName + } + } +} + +console.log( obj.person.sayHi() ); + + +var newObj = { + fullName: "Harry Potter", + sayHi() { return `This person's name is ${this.fullName}`; } + } + +console.log( newObj.sayHi() ); + +var newerObj = { + fullName: "Harry Potter",} + +function sayHi() { return `This person's name is ${this.fullName}`; } + +console.log( sayHi.call(obj)); +console.log( sayHi.call(newObj)); +console.log( sayHi.call(newerObj)); + \ No newline at end of file diff --git a/call_apply_bind_exercise/readme.md b/call_apply_bind_exercise/readme.md index cbc3cd03..3b480801 100644 --- a/call_apply_bind_exercise/readme.md +++ b/call_apply_bind_exercise/readme.md @@ -13,6 +13,37 @@ var obj = { } ``` +```javascript +var obj = { + fullName: "Harry Potter", + person: { + sayHi: function(){ + return "This person's name is " + obj.fullName + } + } +} + +console.log( obj.person.sayHi() ); + + +var newObj = { + fullName: "Harry Potter", + sayHi() { return `This person's name is ${this.fullName}`; } + } + +console.log( newObj.sayHi() ); + +var newerObj = { + fullName: "Harry Potter",} + +function sayHi() { return `This person's name is ${this.fullName}`; } + +console.log( sayHi.call(obj)); +console.log( sayHi.call(newObj)); +console.log( sayHi.call(newerObj)); + +``` + - List two examples of "array-like-objects" that we have seen. - - From fb38d6d52af54c1eb908780858d7b010be9659fd Mon Sep 17 00:00:00 2001 From: toChaim Date: Wed, 9 Aug 2017 19:07:30 -0700 Subject: [PATCH 18/24] finished tests for guessingGame and passed tests. --- call_apply_bind_exercise/callApplyBind.js | 62 +++++++++++++------ call_apply_bind_exercise/callApplyBindSpec.js | 59 ++++++++++++++++++ call_apply_bind_exercise/readme.md | 4 +- 3 files changed, 103 insertions(+), 22 deletions(-) diff --git a/call_apply_bind_exercise/callApplyBind.js b/call_apply_bind_exercise/callApplyBind.js index ef7ecca0..fcc4817a 100644 --- a/call_apply_bind_exercise/callApplyBind.js +++ b/call_apply_bind_exercise/callApplyBind.js @@ -1,28 +1,50 @@ -var obj = { - fullName: "Harry Potter", - person: { - sayHi: function(){ - return "This person's name is " + obj.fullName - } - } +"use strict" + +function sumEvenArguments(){ + var arr = [].slice.call(arguments); + return arr.reduce( + function(t, v){ + if(v % 2 === 0) { return t+v; } + else { return t;} + },0); } -console.log( obj.person.sayHi() ); +function arrayFrom(obj){ + return [].slice.call(obj); + //return Array.prototype.slice.call(obj); + /* + var arr = []; + + for(let i = 0; i < obj.length; i++){ + arr.push(obj[i]); + } + return arr; + */ +} -var newObj = { - fullName: "Harry Potter", - sayHi() { return `This person's name is ${this.fullName}`; } - } +function invokeMax(fn, max){ + var count = 0; -console.log( newObj.sayHi() ); + return function(){ + if(++count > max) return `Maxed Out!`; + var arr = [].slice.call(arguments); -var newerObj = { - fullName: "Harry Potter",} + return fn.apply(this, arr); + }; -function sayHi() { return `This person's name is ${this.fullName}`; } +} -console.log( sayHi.call(obj)); -console.log( sayHi.call(newObj)); -console.log( sayHi.call(newerObj)); - \ No newline at end of file +function guessingGame(amount){ + var count = 0; + var answer = Math.floor(Math.random() * 11); + + return function(guess){ + if(count++ === amount) { return `No more guesses the answer was ${answer}`; } + else if(count > amount) { return `You are all done playing!`; } + else if(answer > guess){ return "You're too low!"; } + else if(answer < guess){ return "You're too high!"; } + else if(answer === guess){ return "You got it!"; } + } + +} diff --git a/call_apply_bind_exercise/callApplyBindSpec.js b/call_apply_bind_exercise/callApplyBindSpec.js index 609e3ff0..08037827 100644 --- a/call_apply_bind_exercise/callApplyBindSpec.js +++ b/call_apply_bind_exercise/callApplyBindSpec.js @@ -32,4 +32,63 @@ describe("#invokeMax", function(){ expect(addOnlyThreeTimes(1,2)).to.equal("Maxed Out!") expect(addOnlyThreeTimes(1,2)).to.equal("Maxed Out!") }); +}); + +describe("#guessingGame", function(){ + + it("shuould return the right type", function(){ + var game = guessingGame(0); + expect(typeof game).to.equal('function') + expect(typeof game(0)).to.equal('string') + }); + + it("inner function should return the correct results", function(){ + var game = guessingGame(0); + expect(game(0).substring(0,31)).to.equal('No more guesses the answer was ') + expect(game(0)).to.equal('You are all done playing!') + expect(game(-1)).to.equal('You are all done playing!') + game = guessingGame(10); + for(let t = 0; t < 10; t++){ + expect(game(-1)).to.equal("You're too low!") + } + expect(game(-1).substring(0,31)).to.equal('No more guesses the answer was ') + expect(game(-1)).to.equal('You are all done playing!') + expect(game(-1)).to.equal('You are all done playing!') + game = guessingGame(12); + for(let t = 0; t < 12; t++){ + expect(game(11)).to.equal("You're too high!") + } + expect(game(11).substring(0,31)).to.equal('No more guesses the answer was ') + expect(game(11)).to.equal('You are all done playing!') + expect(game(-1)).to.equal('You are all done playing!') + game = guessingGame(11) + var gotWin = false; + for(let t = 0; t < 11; t++){ + if(game(t) === "You got it!"){ + gotWin = true; + break; + } + } + expect(gotWin).to.equal(true) + }); + + it("should have choose every possiblity and change", function(){ + var cntObj = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0,}; + for(let t = 0; t < 1000000; t++){ + let game = guessingGame(11); + for(let n = 0; n < 11; n++){ + if(game(n) === "You got it!"){ + cntObj[n]++; + break; + } + } + + // return false or expect(flase).to.equal(true) + + } + for(let n = 0; n < 11; n++){ + expect(cntObj[n] > 10).to.equal(true) + } + }); + }); \ No newline at end of file diff --git a/call_apply_bind_exercise/readme.md b/call_apply_bind_exercise/readme.md index 3b480801..4377f03c 100644 --- a/call_apply_bind_exercise/readme.md +++ b/call_apply_bind_exercise/readme.md @@ -45,8 +45,8 @@ console.log( sayHi.call(newerObj)); ``` - List two examples of "array-like-objects" that we have seen. - - - - + - arguments + - jQuery selectors like $('p'); ### Functions to write: From 05e9cceb4f49dfd4f807ce2dc0fe51f4f2028ee8 Mon Sep 17 00:00:00 2001 From: toChaim Date: Thu, 10 Aug 2017 15:39:42 -0700 Subject: [PATCH 19/24] finish part 1 and 2 original. --- prototypes_exercise/prototypes.js | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/prototypes_exercise/prototypes.js b/prototypes_exercise/prototypes.js index e69de29b..661f6676 100644 --- a/prototypes_exercise/prototypes.js +++ b/prototypes_exercise/prototypes.js @@ -0,0 +1,65 @@ +function Person( + firstName, lastName, favoriteColor, favoriteNumber, favoriteFoods =[] + ) +{ //"Elie", "Schoppik", "purple", 34 + this.firstName = firstName; + this.lastName = lastName; + this.favoriteColor = favoriteColor; + this.favoriteNumber = favoriteNumber; + this.favoriteFoods = favoriteFoods; + this.fullName = function(){ return `${this.firstName} ${this.lastName}`;}; + this.family = []; +} + +Person.prototype.toString = function() + { return `${this.fullName()}, Favorite Color: ${this.favoriteColor}, Favorite Number: ${this.favoriteNumber}` }; +Person.prototype.addToFamily = function(fm){ + if(this.family.includes(fm)) return this.family.length; + if(fm instanceof Person === false) return this.family.length; + + this.family.push(fm); + + return this.family.length; +}; + +Array.prototype.map = function(fn, keepUndefined = false){ + var newArr = []; + + for(let i = 0; i < this.length; i++){ + if(this[i] !== undefined || keepUndefined){ + newArr.push(fn(this[i], i, this)); + } + } + + return newArr; +} + +String.prototype.reverse = function(){ + return this.split('').reverse().join(''); +} + +function rev(str){ + return str.split('').reverse().join(''); +} + +/*Function.prototype.bind = function(thisArg, ...args){ + var fnThis = this; + + return function(...newArgs){ + return fnThis.apply(thisArg, args.concat(newArgs)); + } + +}*/ + +Function.prototype.bind = function(thisArg, ...args){ + var fnThis = this; + + return function(...newArgs){ + //return fnThis.apply(thisArg, args.concat(newArgs)); + thisArg.__TempFunction__ = fnThis; + let temp = thisArg.__TempFunction__(...args,...newArgs); + delete thisArg.__TempFunction__ ; + return temp; + } + +} \ No newline at end of file From f4d98df4b61d82f32b241d4ca1764b6f82ac20f2 Mon Sep 17 00:00:00 2001 From: toChaim Date: Thu, 10 Aug 2017 16:24:54 -0700 Subject: [PATCH 20/24] finish part 1 & 2 --- prototypes_exercise/prototypes.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/prototypes_exercise/prototypes.js b/prototypes_exercise/prototypes.js index 661f6676..7991205c 100644 --- a/prototypes_exercise/prototypes.js +++ b/prototypes_exercise/prototypes.js @@ -62,4 +62,17 @@ Function.prototype.bind = function(thisArg, ...args){ return temp; } +} + +Array.prototype.reduce = function(fn, init, keepUndefined = false){ + for(let i = 0; i < this.length; i++){ + if(i === 0 && init === undefined){ + init = this[0]; + continue; + }else if(this[i] !== undefined || keepUndefined){ + init = fn(init, this[i], i, this); + } + } + + return init; } \ No newline at end of file From ed8a7e3d24f3858bdb97a015477f44629b12adeb Mon Sep 17 00:00:00 2001 From: toChaim Date: Fri, 11 Aug 2017 01:16:45 -0700 Subject: [PATCH 21/24] tic,tac,toe working --- ajax_with_jquery_exercise/index.js | 133 ++++++++---------------- intermediate_oop/tic_tac_toe/index.html | 6 +- intermediate_oop/tic_tac_toe/js/main.js | 132 ++++++++++++++++++++++- 3 files changed, 180 insertions(+), 91 deletions(-) diff --git a/ajax_with_jquery_exercise/index.js b/ajax_with_jquery_exercise/index.js index e6d55841..09c0c105 100644 --- a/ajax_with_jquery_exercise/index.js +++ b/ajax_with_jquery_exercise/index.js @@ -1,103 +1,62 @@ $(document).ready(function(){ - var $links = $('#links'); - var linksList = []; - var favList = []; - var $login_link = $('#login-link'); - var $login_form = $('#login-form'); - var $inp_email = $('#inp-email'); - var $inp_password = $('#inp-password'); - var authorization = ''; - - $('#login-link').click(() => { $login_form .toggleClass("hidden"); }); - $('#login-btn').click(function(e){ - e.preventDefault(); - //var email = "lane.matthew@gmail.com"; - var email = $inp_email.val(); - //var password = "password"; - var password = $inp_password.val(); - - $.ajax({ - method: "POST", - headers: { - "Content-Type": "application/json" - }, - url: "https://hn-favorites.herokuapp.com/login", - data: JSON.stringify({ - 'email': email, - 'password': password - }) - }) - .then(function(data) { - localStorage.setItem('auth_token', data.auth_token); - authorization = data.auth_token; - console.log(data.auth_token); - $login_link.text('Log Out'); - $inp_email.val(''); - $inp_password.val(''); - $login_form .toggleClass("hidden"); - return $.ajax( - { - method: "GET", - headers: { "Authorization": authorization }, - 'url': 'https://hn-favorites.herokuapp.com/stories.json', - }); - }) - .then(function(data){ - getStories(true, data); - }) - .fail(err => console.warn(err)); - }); - - getStories(); - - function getStories(login = false, favorites = []){ - var url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'; - - $.get(url) - .then(function(res){ - $links.empty(); - for(let i = 0; i < 20; i++){ - var newUrl = `https://hacker-news.firebaseio.com/v0/item/${res[i]}.json?print=pretty`; - $.get(newUrl).then(function(res){ - displayLink(res.title, res.url, res.id, false, login); - }); - } - + //variables + var auth_token; + + //imeadiat calls + getTop().then(function(data){ + debugger + }) + + //click functions + + //functions + function getTop(){ + return $.get('https://hacker-news.firebaseio.com/v0/' + + 'topstories.json?print=pretty') + .then(function(topstories){ + console.log(topstories); + return Promise.all(topstories.slice(0,50).map(function(v,i){ + return $.get( + `https://hacker-news.firebaseio.com/v0/' + + 'item/${topstories[i]}.json?print=pretty`) + })); }) .fail(function(err){ - console.warn(err); + console.warn(err); + return err; }); + } - function getFavorites(login) { - var url = 'https://hn-favorites.herokuapp.com/stories.json'; - - $.ajax({ + function getFav() { + return $.ajax({ method: "GET", headers: { - "Authorization": authorization, + "Authorization": auth_token, }, - 'url': url, - }) - .then(function(data) { + 'url': 'https://hn-favorites.herokuapp.com/stories.json', }) - .fail(err => console.warn(err)); } - function displayLink(title, link, storyId=0, favor = false, login = false){ - var loginClass = 'hideNoLogin'; - if(login) loginClass = 'showOnLogin'; - let html = ``; - let shortLink = ''; - if(link) { shortLink = link.split('/').filter(v => v.includes('.')); } - if(favor) html = ``; - { html += `${title} ( ${shortLink[0]} )`; } - let $lin = $('
  • '); - $lin.html(html); - if(favor) $lin.addClass('favor'); - $lin.addClass('bg-warning') - $links.append($lin); + function getLogin(email, password){ + $.ajax({ + method: "POST", + headers: { + "Content-Type": "application/json" + }, + url: "https://hn-favorites.herokuapp.com/login", + data: JSON.stringify({ + 'email': email, + 'password': password + }) + }).then(function(data) { + localStorage.setItem('auth_token', data.auth_token); + auth_token = data.auth_token; + console.log(data.auth_token); + }) + + } }); \ No newline at end of file diff --git a/intermediate_oop/tic_tac_toe/index.html b/intermediate_oop/tic_tac_toe/index.html index 00c74eb9..01f283ba 100644 --- a/intermediate_oop/tic_tac_toe/index.html +++ b/intermediate_oop/tic_tac_toe/index.html @@ -11,9 +11,9 @@ - - - + + +

    Tic Tac Toe

    diff --git a/intermediate_oop/tic_tac_toe/js/main.js b/intermediate_oop/tic_tac_toe/js/main.js index 379d891d..63f423bd 100644 --- a/intermediate_oop/tic_tac_toe/js/main.js +++ b/intermediate_oop/tic_tac_toe/js/main.js @@ -1,2 +1,132 @@ document.addEventListener("DOMContentLoaded", function() { -}); + //Imediate Calls + var game = new Game(); + + //set up web clicks + $('#new-game').click(function(){ + game.reset(); + }) + + + //Functions + function Game(){ + this.player = 'X'; + this.board = new Board(this); + this.gameOn = true; + } + Game.prototype.reset = function(){ + this.player = 'X'; + this.board.clear(); + this.gameOn = true; + } + Game.prototype.play = function(row, col){ + //check for leagal move + if(this.gameOn && this.board.isEmpty(row,col)){ + //mark play + this.board.play(row,col,this.player); + //check for win or tie + if(this.winLooseOrDraw()){ + this.gameOn = false; + alert(this.winLooseOrDraw()); + } + //change players + this.player = (this.player === 'X'? 'O':'X'); + } + } + Game.prototype.winLooseOrDraw = function(){ + let ties = 0; + for(let row = 0; row < 3; row++){ + let count = {X:0, O:0, "":0}; + for(let col = 0; col < 3; col++){ + count[this.board.get(row, col)]++; + } + if(count['X']>0 && count['O']>0) ties++; + else if(count['X'] === 3) return 'X Wins!'; + else if(count['O'] === 3) return 'O Wins!'; + //console.log(count,ties); + } + for(let col = 0; col < 3; col++){ + let count = {X:0, O:0, "":0}; + for(let row = 0; row < 3; row++){ + count[this.board.get(row, col)]++; + } + if(count['X']>0 && count['O']>0) ties++; + else if(count['X'] === 3) return 'X Wins!'; + else if(count['O'] === 3) return 'O Wins!'; + //console.log(count,ties); + } + let count = {X:0, O:0, "":0}; + for(let row = 0, col=0; row < 3; row++, col++){ + count[this.board.get(row, col)]++; + } + if(count['X']>0 && count['O']>0) ties++; + else if(count['X'] === 3) return 'X Wins!'; + else if(count['O'] === 3) return 'O Wins!'; + count = {X:0, O:0, "":0}; + for(let row = 2, col=0; row >= 0; row--, col++){ + count[this.board.get(row, col)]++; + } + if(count['X']>0 && count['O']>0) ties++; + else if(count['X'] === 3) return 'X Wins!'; + else if(count['O'] === 3) return 'O Wins!'; + + if(ties === 8) return 'Tie Game'; + else return false; + } + + function Board(game){ + this.game = game; + this.$board = $('#board'); + this.squares = []; + + for(let r = 0; r < 3; r++){ + let row = []; + for(let col = 0; col < 3; col++){ + row.push(new Squares($(`#square_${r}_${col}`))); + } + this.squares.push(row); + } + + //help binding is not working + //var play = game.play.bind(this); + //console.log(this.game); + this.$board.on('click', '.square', function(e){ + var [row, col] = e.target.id.split('_').slice(1); + game.play(row,col); + }); + + } + Board.prototype.clear = function(){ + for(let row = 0; row < 3; row++){ + for(let col = 0; col < 3; col++){ + this.squares[row][col].clear(); + } + } + } + Board.prototype.get = function(row, col){ + return this.squares[row][col].get(); + } + Board.prototype.isEmpty = function(row, col){ + return this.squares[row][col].isEmpty(); + } + Board.prototype.play = function(row, col, player){ + this.squares[row][col].set(player); + } + + function Squares($jObj){ + this.$jObj = $jObj; + } + Squares.prototype.clear = function(){ + this.$jObj.text(''); + } + Squares.prototype.isEmpty = function(){ + return this.$jObj.text() === ''; + } + Squares.prototype.set = function(val){ + this.$jObj.text(val); + } + Squares.prototype.get = function(){ + return this.$jObj.text(); + } + +}); \ No newline at end of file From fd0df58bf0fa9194ba2d89bebdb13be729802856 Mon Sep 17 00:00:00 2001 From: toChaim Date: Fri, 11 Aug 2017 11:31:34 -0700 Subject: [PATCH 22/24] refactoring tic tac toe --- intermediate_oop/tic_tac_toe/js/main.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/intermediate_oop/tic_tac_toe/js/main.js b/intermediate_oop/tic_tac_toe/js/main.js index 63f423bd..21479e43 100644 --- a/intermediate_oop/tic_tac_toe/js/main.js +++ b/intermediate_oop/tic_tac_toe/js/main.js @@ -92,16 +92,17 @@ document.addEventListener("DOMContentLoaded", function() { //console.log(this.game); this.$board.on('click', '.square', function(e){ var [row, col] = e.target.id.split('_').slice(1); - game.play(row,col); - }); + this.game.play(row,col); + }.bind(this)); } Board.prototype.clear = function(){ - for(let row = 0; row < 3; row++){ - for(let col = 0; col < 3; col++){ - this.squares[row][col].clear(); - } - } + $('.squares').text('').removeClass('X O'); + // for(let row = 0; row < 3; row++){ + // for(let col = 0; col < 3; col++){ + // this.squares[row][col].clear(); + // } + // } } Board.prototype.get = function(row, col){ return this.squares[row][col].get(); @@ -124,6 +125,7 @@ document.addEventListener("DOMContentLoaded", function() { } Squares.prototype.set = function(val){ this.$jObj.text(val); + this.$jObj.addClass(val); } Squares.prototype.get = function(){ return this.$jObj.text(); From c70ee4204f3c31849c7b0620f01f3b4ab7715f89 Mon Sep 17 00:00:00 2001 From: toChaim Date: Fri, 11 Aug 2017 13:52:52 -0700 Subject: [PATCH 23/24] fix message. Calling myself done. --- intermediate_oop/tic_tac_toe/css/main.css | 15 +++++++++- intermediate_oop/tic_tac_toe/index.html | 4 ++- intermediate_oop/tic_tac_toe/js/main.js | 34 ++++++++++++++--------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/intermediate_oop/tic_tac_toe/css/main.css b/intermediate_oop/tic_tac_toe/css/main.css index c009bbb2..0f362e13 100644 --- a/intermediate_oop/tic_tac_toe/css/main.css +++ b/intermediate_oop/tic_tac_toe/css/main.css @@ -8,7 +8,7 @@ body { width: 185px; height: 165px; margin: 3px; - background-color: #bfbfbf; + border: 7px solid #bfbfbf; vertical-align: top; font-size: 700%; color: red; @@ -24,4 +24,17 @@ body { #message { font-size: 3em; +} +#board{ + width: 600px; + height: 525px; + margin: auto; +} +.X{ + color: green; + border: 5px solid green; +} +.O{ + color: blue; + border: 5px solid blue; } \ No newline at end of file diff --git a/intermediate_oop/tic_tac_toe/index.html b/intermediate_oop/tic_tac_toe/index.html index 01f283ba..6102a83c 100644 --- a/intermediate_oop/tic_tac_toe/index.html +++ b/intermediate_oop/tic_tac_toe/index.html @@ -17,7 +17,8 @@

    Tic Tac Toe

    -
    +
    +
    @@ -34,6 +35,7 @@

    Tic Tac Toe

    +

    diff --git a/intermediate_oop/tic_tac_toe/js/main.js b/intermediate_oop/tic_tac_toe/js/main.js index 21479e43..613ee8cc 100644 --- a/intermediate_oop/tic_tac_toe/js/main.js +++ b/intermediate_oop/tic_tac_toe/js/main.js @@ -11,12 +11,15 @@ document.addEventListener("DOMContentLoaded", function() { //Functions function Game(){ this.player = 'X'; + this.message = new Message(); this.board = new Board(this); this.gameOn = true; } Game.prototype.reset = function(){ this.player = 'X'; this.board.clear(); + this.message.clear(); + this.board.changePlayer(this.player); this.gameOn = true; } Game.prototype.play = function(row, col){ @@ -27,10 +30,12 @@ document.addEventListener("DOMContentLoaded", function() { //check for win or tie if(this.winLooseOrDraw()){ this.gameOn = false; - alert(this.winLooseOrDraw()); + this.message.set(this.winLooseOrDraw()); } //change players this.player = (this.player === 'X'? 'O':'X'); + this.board.changePlayer(this.player); + //this.board. } } Game.prototype.winLooseOrDraw = function(){ @@ -87,9 +92,6 @@ document.addEventListener("DOMContentLoaded", function() { this.squares.push(row); } - //help binding is not working - //var play = game.play.bind(this); - //console.log(this.game); this.$board.on('click', '.square', function(e){ var [row, col] = e.target.id.split('_').slice(1); this.game.play(row,col); @@ -97,12 +99,7 @@ document.addEventListener("DOMContentLoaded", function() { } Board.prototype.clear = function(){ - $('.squares').text('').removeClass('X O'); - // for(let row = 0; row < 3; row++){ - // for(let col = 0; col < 3; col++){ - // this.squares[row][col].clear(); - // } - // } + $('.square').text('').removeClass('X O'); } Board.prototype.get = function(row, col){ return this.squares[row][col].get(); @@ -113,13 +110,14 @@ document.addEventListener("DOMContentLoaded", function() { Board.prototype.play = function(row, col, player){ this.squares[row][col].set(player); } + Board.prototype.changePlayer = function(player){ + this.$board.removeClass('X O'); + this.$board.addClass(player); + } function Squares($jObj){ this.$jObj = $jObj; } - Squares.prototype.clear = function(){ - this.$jObj.text(''); - } Squares.prototype.isEmpty = function(){ return this.$jObj.text() === ''; } @@ -131,4 +129,14 @@ document.addEventListener("DOMContentLoaded", function() { return this.$jObj.text(); } + function Message(){ + this.$message = $('#message'); + } + Message.prototype.set = function(msg){ + this.$message.text(msg); + } + Message.prototype.clear = function(msg){ + this.$message.text(''); + } + }); \ No newline at end of file From 854a393403d99141cd0b878011836e2edcfb251d Mon Sep 17 00:00:00 2001 From: toChaim Date: Tue, 15 Aug 2017 09:59:43 -0700 Subject: [PATCH 24/24] refactor hacker snooze. Almost Done --- ajax_with_jquery_exercise/index.css | 9 ++ ajax_with_jquery_exercise/index.html | 10 +- ajax_with_jquery_exercise/index.js | 175 ++++++++++++++++++++++++--- 3 files changed, 174 insertions(+), 20 deletions(-) diff --git a/ajax_with_jquery_exercise/index.css b/ajax_with_jquery_exercise/index.css index 01029487..24445857 100644 --- a/ajax_with_jquery_exercise/index.css +++ b/ajax_with_jquery_exercise/index.css @@ -1,6 +1,15 @@ .row{ margin-bottom: 30px; } +#btn_group{ + width: 100%; + display: flex; + flex-direction: + row; justify-content: space-around; +} +form button{ + width: 100px; +} .glyphicon{ margin: 5px; } diff --git a/ajax_with_jquery_exercise/index.html b/ajax_with_jquery_exercise/index.html index b918d3f6..29f30c20 100644 --- a/ajax_with_jquery_exercise/index.html +++ b/ajax_with_jquery_exercise/index.html @@ -19,7 +19,7 @@