From e020f7bc72ad3867329d37e32b80e001bf71b5ca Mon Sep 17 00:00:00 2001 From: Chitwant Date: Mon, 31 Jul 2017 17:31:42 -0700 Subject: [PATCH 01/17] function 1 sol --- testing_exercise/testing.js | 10 +++++++++ testing_exercise/testingSpec.js | 39 ++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index e69de29b..b7bb74bc 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -0,0 +1,10 @@ +function replaceWith(entry, replaceChar, withChar) { + if (entry.indexOf(replaceChar) !== -1) { + console.log("FOR Loop") + for (var i=0; i < entry.length; i++) + if (entry[i] === replaceChar) + entry = entry.substr(0, i) + withChar + entry.substr(i + 1, entry.length - 1); + } + + return entry; +} \ No newline at end of file diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index aef56b1d..7bcdd9d4 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -1,3 +1,40 @@ var expect = chai.expect; -// WRITE YOUR TESTS HERE! \ No newline at end of file +// WRITE YOUR TESTS HERE! +describe("TESTS FOR replaceWith", function() { + it("happy path, character present twice", function() { + expect(replaceWith("awesome", "e", "z")).to.equal("awzsomz"); + }); + + it("case sensitive single character", function() { + expect(replaceWith("Foo", "F", "B")).to.equal("Boo"); + }); + + it("case sensitive multiple character", function() { + expect(replaceWith("FooF", "F", "B")).to.equal("BooB"); + }); + + it("case sensitive single character with char present in lower and upper case", function() { + expect(replaceWith("Foof", "F", "B")).to.equal("Boof"); + }); + + it("character not present", function() { + expect(replaceWith("awesome", "p", "z")).to.equal("awesome"); + }); + + it("character not present case sensitive", function() { + expect(replaceWith("awesome", "E", "Z")).to.equal("awesome"); + }); + + it("input string empty", function() { + expect(replaceWith("", "e", "z")).to.equal(""); + }); + + it("replace with Char empty", function() { + expect(replaceWith("awesome", "", "z")).to.equal("awesome"); + }); +}); + +describe("TESTS FOR expand", function() { + +}); \ No newline at end of file From abfac49e4ba3f4d59fdb8962c71307406b89c2a5 Mon Sep 17 00:00:00 2001 From: Chitwant Date: Mon, 31 Jul 2017 17:55:34 -0700 Subject: [PATCH 02/17] function 1+2 sol --- testing_exercise/testing.js | 12 ++++++++++++ testing_exercise/testingSpec.js | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index b7bb74bc..41b08190 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -7,4 +7,16 @@ function replaceWith(entry, replaceChar, withChar) { } return entry; +} + +function expand(arr, num) { + if (num <=0) + return arr; + + var nArr = []; + for(var i=0; i < num; i++) + for(var j=0; j < arr.length; j++) + nArr.push(arr[j]); + + return nArr; } \ No newline at end of file diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index 7bcdd9d4..2f5e6af2 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -36,5 +36,16 @@ describe("TESTS FOR replaceWith", function() { }); describe("TESTS FOR expand", function() { - -}); \ No newline at end of file + it("happy path, expand integer array 3 times", function() { + expect(expand([1,2,3],3)).to.deep.equal([1,2,3,1,2,3,1,2,3]); + }); + + it("happy path, expand string array 3 times", function() { + expect(expand(["foo", "test"],1)).to.deep.equal(["foo","test"]); + }); + + it("expand array 0 times", function() { + expect(expand(["foo", "test"],0)).to.deep.equal(["foo","test"]); + }); +}); + From 5698a00643b14341bb38e4efc2623dc07fd93786 Mon Sep 17 00:00:00 2001 From: Chitwant Date: Mon, 31 Jul 2017 18:52:42 -0700 Subject: [PATCH 03/17] function 1+2+3+4 sol --- testing_exercise/testing.js | 29 +++++++++++++++++-- testing_exercise/testingSpec.js | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index 41b08190..a8ba0cf8 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -1,6 +1,5 @@ function replaceWith(entry, replaceChar, withChar) { if (entry.indexOf(replaceChar) !== -1) { - console.log("FOR Loop") for (var i=0; i < entry.length; i++) if (entry[i] === replaceChar) entry = entry.substr(0, i) + withChar + entry.substr(i + 1, entry.length - 1); @@ -19,4 +18,30 @@ function expand(arr, num) { nArr.push(arr[j]); return nArr; -} \ No newline at end of file +} + +function acceptNumbersOnly(...nums) { + for(var i=0; i < nums.length; i++) { + if (isNaN(nums[i])) + return false; + else if(typeof nums[i] !== 'number') + return false; + } + + return true; +} + +function mergeArrays(arr1, arr2) { + if (arr1.length === 0 && arr2.length === 0) + return []; + + if(typeof(arr1[0]) === 'number' || typeof(arr2[0]) === 'number') + return arr1.concat(arr2).sort(function(a,b){return a - b}); + else + return arr1.concat(arr2).sort(); +} + +function mergeObjects(obj1, obj2) { + var retObj = {}; + return retObj; +} diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index 2f5e6af2..d03b126b 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -49,3 +49,54 @@ describe("TESTS FOR expand", function() { }); }); +describe("TESTS FOR acceptNumbersOnly", function() { + it("number and string present", function() { + expect(acceptNumbersOnly(1,"foo")).to.be.false; + }); + + it("numbers present only", function() { + expect(acceptNumbersOnly(1,2,3,4,5,6,7)).to.be.true; + }); + + it("number and NaN present", function() { + expect(acceptNumbersOnly(1,2,3,4,5,6,NaN)).to.be.false; + }); +}); + +describe("TESTS FOR mergeArrays", function() { + it("two number arrays", function() { + expect(mergeArrays([2,1],[3,4])).to.deep.equal([1,2,3,4]); + }); + + it("two number arrays unequal length", function() { + expect(mergeArrays([2,1],[3,4,7])).to.deep.equal([1,2,3,4,7]); + }); + + it("two string arrays unequal length", function() { + expect(mergeArrays(["a","c"],["b","d"])).to.deep.equal(["a","b","c","d"]); + }); + + it("two string arrays unequal length", function() { + expect(mergeArrays(["a","c"],["f","b","d"])).to.deep.equal(["a","b","c","d","f"]); + }); + + it("one empty array", function() { + expect(mergeArrays([],[3,4])).to.deep.equal([3,4]); + }); + + it("two empty arrays", function() { + expect(mergeArrays([],[])).to.deep.equal([]); + }); + +}); + +describe("TESTS FOR mergeObjects", function() { + it("two objects", function() { + var obj1 = {name: "Foo", num: 3}; + var obj2 = {test: "thing",num: 55}; + var obj3 = {name: "Foo",test: "thing",num: 55}; + expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); + }); + +}); + From c1b0bc7d4e71a5a4a359fa8cc963a77991e3899d Mon Sep 17 00:00:00 2001 From: Chitwant Date: Mon, 31 Jul 2017 21:21:20 -0700 Subject: [PATCH 04/17] function 1+2+3+4+5 sol --- testing_exercise/testing.js | 6 ++++-- testing_exercise/testingSpec.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index a8ba0cf8..29990f7c 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -42,6 +42,8 @@ function mergeArrays(arr1, arr2) { } function mergeObjects(obj1, obj2) { - var retObj = {}; - return retObj; + for (var key in obj2) { + obj1[key] = obj2[key]; + } + return obj1; } diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index d03b126b..0c39f9f3 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -98,5 +98,26 @@ describe("TESTS FOR mergeObjects", function() { expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); }); + it("one empty object", function() { + var obj1 = {}; + var obj2 = {test: "thing",num: 55}; + var obj3 = {test: "thing",num: 55}; + expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); + }); + + it("both empty", function() { + var obj1 = {}; + var obj2 = {}; + var obj3 = {}; + expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); + }); + + it("2 override, 1 extra field", function() { + var obj1 = {name: "Foo", num: 3}; + var obj2 = {name: "Bar", num: 55, field: "extra"}; + var obj3 = {name: "Bar",num: 55,field: "extra"}; + expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); + }); + }); From f6b21f495da8797b0dfeee5f983e9debcc43a3be Mon Sep 17 00:00:00 2001 From: Chitwant Date: Tue, 1 Aug 2017 15:45:28 -0700 Subject: [PATCH 05/17] recursion 1+2+3 sol --- recursion_exercise/recursion.js | 45 +++++++++++++++++++++++++++++++++ testing_exercise/testing.js | 3 +-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index e69de29b..9b27cbf1 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -0,0 +1,45 @@ +function productOfArray(arr) { + if (arr.length === 1) { + return arr[0]; + } + + return arr[0] * productOfArray(arr.slice(1)); +} + +function collectStrings(obj) { + var nArr = []; + function helper(a) { + var kArr = Object.keys(a); + for (var i=0; i Date: Tue, 1 Aug 2017 16:46:16 -0700 Subject: [PATCH 06/17] recursion 1+2+3+4 sol --- recursion_exercise/recursion.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index 9b27cbf1..d13f0923 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -29,10 +29,11 @@ function contains(obj, num) { var ret = false; function helper(a) { var kArr = Object.keys(a); - for (var i=0; i Date: Tue, 1 Aug 2017 23:00:03 -0700 Subject: [PATCH 07/17] recursion 1+2+3+4 sol simple and helper --- recursion_exercise/recursion.js | 120 +++++++++++++++++++++++++++----- testing_exercise/testing.js | 10 ++- 2 files changed, 107 insertions(+), 23 deletions(-) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index d13f0923..2151033f 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -25,24 +25,109 @@ function collectStrings(obj) { return nArr; } +// contains with helper recursion +// function contains(obj, num) { +// var ret = false; +// function helper(a) { +// var kArr = Object.keys(a); + +// if (kArr.length === 0) +// return; + +// for (var i=0; i Date: Wed, 2 Aug 2017 13:14:10 -0700 Subject: [PATCH 08/17] recursion 1+2+3+4+6 --- recursion_exercise/recursion.js | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index 2151033f..debd1c76 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -150,5 +150,41 @@ function search(arr, num) { function binarySearch(arr, num) { + var idx = -1; + var minIndex = 0; + var maxIndex = arr.length -1; + function helper(a) { + var mid = Math.floor(maxIndex - minIndex / 2); + + if(a[mid] > num) { + maxIndex = mid - 1; + helper(a); + } + else if (a[mid] < num) { + minIndex = mid + 1; + helper(a); + } + else { + return mid; + } + } + idx = helper(arr); + return idx; +} + +function stringifyNumbers(obj) { + var kArr = Object.keys(obj); + + if (kArr.length === 0) + return; + + for (var i=0; i Date: Wed, 2 Aug 2017 13:15:10 -0700 Subject: [PATCH 09/17] recursion 1+2+3+4+6 sol --- recursion_exercise/recursion.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index debd1c76..c2770454 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -172,19 +172,23 @@ function binarySearch(arr, num) { return idx; } +// simple recursion function stringifyNumbers(obj) { + var nObj = {}; var kArr = Object.keys(obj); if (kArr.length === 0) - return; + return obj; for (var i=0; i Date: Thu, 3 Aug 2017 08:46:05 -0700 Subject: [PATCH 10/17] loadash 1-15 sol --- lodash_exercise/lodash.js | 219 ++++++++++++++++++++++++++++++++++---- 1 file changed, 199 insertions(+), 20 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 483d734a..3a02eeb8 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,61 +1,240 @@ -function drop(){ +function drop(arr, n=1){ + var nArr = []; + for(var i=n; i arr.length) { + num--; + } -} - -function includes(){ + nArr = arr.slice(arr.length - num); + return nArr; } -function sample(){ +function union(...arrays){ + var rSet = new Set(); + var nArr = []; -} + for(var i=0; i values.length -1) + nObj[props[i]] = undefined; + nObj[props[i]] = values[i]; + } + return nObj; } -function inRange(){ +function includes(a, val, idx=0){ + if (typeof a === 'string') { + if(a.indexOf(val) !== -1) + return true; + } + else if (Array.isArray(a)) { + if (idx > 0) { + var nArr = a.slice(idx); + if(nArr.indexOf(val) > -1) + return true; + } + else { + if(a.indexOf(val) > -1) + return true; + } + } + else if (typeof a === 'object') { + if (Object.values(a).indexOf(val) > -1) + return true; + } + else if (Array.isArray(a)) { + if (idx > 0) { + var nArr = a.slice(idx); + if(nArr.indexOf(val) > -1) + return true; + } + else + if(a.indexOf(val) > -1) + return true; + } + + return false; +} +function sample(arr){ + var num = Math.floor(Math.random() * arr.length); + return arr[num]; } -function has(){ +function cloneDeep(a){ + if (Array.isArray(a)) { + var nArr = []; + for (var i=0; i < a.length; i++) { + if (Array.isArray(a[i]) || typeof a[i] === 'object') + nArr.push(cloneDeep(a[i])); + else + nArr.push(a[i]); + } + + return nArr; + } + else if (typeof a === 'object') { + var nObj = {}; + for (key in a) { + if ( Array.isArray(a[key]) || typeof a[key] === 'object') + nObj[key] = cloneDeep(a[key]); + } + return nObj; + } +} +function sumBy(arr, second){ + var sum = 0; + if(typeof second === 'function') { + for(var i=0; i end && num < start; + else + return num < end && num > start; +} +function has(obj, gKeys){ + if(typeof gKeys === 'string') + gKeys = gKeys.split('.'); + + var presentKeys = []; + function helper(a) { + for (key in a) { + presentKeys.push(key); + + if(typeof a[key] === 'object') + helper(a[key]); + else + return; + } + } + helper(obj); + + for(var i=0; i Date: Thu, 3 Aug 2017 20:23:45 -0700 Subject: [PATCH 11/17] canvas app sol --- canvas_exercise/shapes_game/index.js | 139 +++++++++++++++++++++++---- 1 file changed, 119 insertions(+), 20 deletions(-) diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 0de5f18a..cbb71b0a 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -1,36 +1,135 @@ window.addEventListener("load", function() { function clear(ctx, width, heigt) { + ctx.clearRect(0, 0, width, height); } - function drawRandomShape(ctx, width, height) { + function drawTraingle(ctx, width, height, color) { + ctx.fillStyle = color; + ctx.beginPath(); + ctx.moveTo(width,height); + ctx.lineTo(width+100, height+100); + ctx.lineTo(width, height+100); + ctx.fill(); + ctx.closePath(); } - function drawGameStartText(ctx, width, height, score) { + function drawRandomShape(shapes) { + var shape = shapes[Math.floor(Math.random()*shapes.length)]; + var randWidth = Math.floor(Math.random() * (700 - 100)) + 100; + var randHeight = Math.floor(Math.random() * (650 - 100)) + 100; + var eKey; + switch(shape) { + case "redT": + clear(ctx, (canvas.width), (canvas.height)); + drawTraingle(ctx, randWidth, randHeight, "red"); + eKey = "redT"; + break; + case "redS": + clear(ctx, (canvas.width), (canvas.height)); + drawSquare(ctx, randWidth, randHeight, 100, 100, "red"); + eKey = "redS"; + break; + case "whiteT": + clear(ctx, (canvas.width), (canvas.height)); + drawTraingle(ctx, randWidth, randHeight, "white"); + eKey = "whiteT"; + break; + case "whiteS": + clear(ctx, (canvas.width), (canvas.height)); + drawSquare(ctx, randWidth, randHeight, 100, 100, "white"); + eKey = "whiteS"; + break; + default: + clear(ctx, (canvas.width), (canvas.height)); + drawTraingle(ctx, randWidth, randHeight, "red"); + eKey = "redT"; + } + return eKey; } - function restartGame(ctx, width, height) { + function drawSquare(ctx, startx, starty, width, height, color) { + var square = { + corner: [startx,starty], + width: width, + height: height, + color: color, + draw: function() { + ctx.fillStyle = this.color; + ctx.fillRect(this.corner[0], this.corner[1], this.width, this.height); + } + } + square.draw(); + } + + function drawGameStartText(ctx, width, height, score) { + ctx.fillStyle = "#FFFFFF"; + ctx.font = '30px san-serif'; + ctx.textBaseline = 'middle'; + ctx.textAlign = 'center'; + ctx.fillText('Press the space bar to start a new game.' , width, height); + ctx.fillText("Score: " + score, width, height + 30); } - 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 - expectedKeysMap = {white0: 38, red1: 40, red0: 37, white1: 39}, - timerSpan = document.getElementById("time-remaining"), - scoreSpan = document.getElementById("score-val"), - seconds = 3, - intervalId; + //. ########################## // + // Loading prep work + //. ########################## // + var canvas = document.getElementById("shapes-game"), + height = canvas.scrollHeight, + width = canvas.scrollWidth, + gameOn = false, + expectedKey = undefined, + shapes = ["whiteS", "whiteT", "redS", "redT"], + ctx = canvas.getContext('2d'), + // white triangle = up, red square = down, + // red triangle = left, white square = right + expectedKeysMap = {whiteT: 38, redS: 40, redT: 37, whiteS: 39}, + timerSpan = document.getElementById("time-remaining"), + scoreSpan = document.getElementById("score-val"), + seconds = 3, + cScore = 0, + intervalId; canvas.width = width; canvas.height = height; + drawGameStartText(ctx, (canvas.width/2), (canvas.height/2), 0); - document.addEventListener("keyup", function() { - - }); -}); + //. ########################## // + // Key up activities + //. ########################## // + document.addEventListener("keyup", function(e) { + if (e.keyCode === 32) { + gameOn = true; + expectedKey = drawRandomShape(shapes); + // start timer + var time = parseInt(timerSpan.innerText); + intervalId = setInterval(function() { + timerSpan.innerText = --time; + if (time === 0) { + clearInterval(intervalId); + // reset gameOn, score and time to default values + gameOn = false; + scoreSpan.innerText = "0"; + timerSpan.innerText = "30"; + // display text + clear(ctx, (canvas.width), (canvas.height)); + drawGameStartText(ctx, (canvas.width/2), (canvas.height/2), cScore); + } + }, 1000); + } + else { // score computations + if(gameOn) { + cScore = parseInt(scoreSpan.innerText); + if (e.keyCode === expectedKeysMap[expectedKey]) + scoreSpan.innerText = ++cScore; + else { + if(cScore > 0) + scoreSpan.innerText = --cScore; + } + + expectedKey = drawRandomShape(shapes); + } + } + }); +}); \ No newline at end of file From d7d53b60b1c8a9822e26e1c6b5b8df722e431459 Mon Sep 17 00:00:00 2001 From: Chitwant Date: Thu, 3 Aug 2017 22:46:15 -0700 Subject: [PATCH 12/17] comments fixed --- lodash_exercise/lodash.js | 2 -- testing_exercise/testingSpec.js | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 3a02eeb8..e5be0a88 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -181,8 +181,6 @@ function has(obj, gKeys){ if(typeof a[key] === 'object') helper(a[key]); - else - return; } } helper(obj); diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index 0c39f9f3..44bccaf2 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -119,5 +119,4 @@ describe("TESTS FOR mergeObjects", function() { expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); }); -}); - +}); \ No newline at end of file From 58f3863d5365e8d6d7734cf0aaac40d0c73d4cff Mon Sep 17 00:00:00 2001 From: Chitwant Date: Thu, 3 Aug 2017 23:03:00 -0700 Subject: [PATCH 13/17] sol 16-17 lodash --- lodash_exercise/lodash.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index e5be0a88..d078d506 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -235,12 +235,20 @@ function pick(obj, gKeys){ return rObj; } -function pickBy(){ - +function pickBy(obj, fn){ + rObj = {}; + for (key in obj) + if (fn(obj[key])) + rObj[key] = obj[key]; + return rObj; } -function omitBy(){ - +function omitBy(obj, fn){ + rObj = {}; + for (key in obj) + if (!fn(obj[key])) + rObj[key] = obj[key]; + return rObj; } function padEnd(){ From ab7f4a1bc72b9c2c11fbd6fe9910433e476acfd9 Mon Sep 17 00:00:00 2001 From: Chitwant Date: Thu, 3 Aug 2017 23:11:50 -0700 Subject: [PATCH 14/17] sol 16-17 lodash updated --- lodash_exercise/lodash.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index d078d506..781d879d 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -248,6 +248,7 @@ function omitBy(obj, fn){ for (key in obj) if (!fn(obj[key])) rObj[key] = obj[key]; + return rObj; } From a17aad41a7911dc7e4cbd12f4f980ed33db547ec Mon Sep 17 00:00:00 2001 From: Chitwant Date: Wed, 9 Aug 2017 19:34:43 -0700 Subject: [PATCH 15/17] call-apply-bind sol --- call_apply_bind_exercise/callApplyBind.js | 68 ++++ lodash_exercise/lodash.js | 407 ++++++++++++---------- 2 files changed, 284 insertions(+), 191 deletions(-) diff --git a/call_apply_bind_exercise/callApplyBind.js b/call_apply_bind_exercise/callApplyBind.js index e69de29b..669461db 100644 --- a/call_apply_bind_exercise/callApplyBind.js +++ b/call_apply_bind_exercise/callApplyBind.js @@ -0,0 +1,68 @@ +var obj = { + fullName: "Harry Potter", + person: { + sayHi: function(){ + return "This person's name is " + this.fullName + }.bind(obj) + } +} + + +function sumEvenArguments() { + var args = [].slice.call(arguments); + + return args.reduce(function(acc, nextValue) { + if( nextValue % 2 === 0 ) acc += nextValue; + return acc; + }, 0); + + // return args.filter(function(a) { return (a % 2) === 0; }).reduce(function(acc, nextValue) { + // return acc + nextValue + // }, 0); +} + +function arrayFrom() { + return [].slice.call(arguments); +} + +function invokeMax(fn, num) { + var count = num; + return function innerFunction(){ + if (count <= 0) { + return "Maxed Out!"; + } + else { + count--; + return fn.apply(this, arguments); + + } + } +} + +function guessingGame(amount) { + var answer = Math.floor(Math.random() * 11); + var guesses = 0; + return function innerFunction(guess) { + if(guesses <= amount) { + if(guesses === amount && guess !== answer) { + guesses++; + return "No more guesses the answer was " + answer; + } + if(guess === answer) { + guesses = amount + 1; + return "You got it!"; + } + if (guess < answer) { + guesses++; + return "You're too low!"; + } + if (guess > answer) { + guesses++; + return "You're too high!"; + } + } + else { + return "You are all done playing!"; + } + } +} \ No newline at end of file diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 781d879d..53850775 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,263 +1,288 @@ function drop(arr, n=1){ - var nArr = []; - for(var i=n; i arr.length) { - num--; - } + var nArr = []; + while (num > arr.length) { + num--; + } - nArr = arr.slice(arr.length - num); + nArr = arr.slice(arr.length - num); - return nArr; + return nArr; } function union(...arrays){ - var rSet = new Set(); - var nArr = []; + var rSet = new Set(); + var nArr = []; - for(var i=0; i values.length -1) - nObj[props[i]] = undefined; - - nObj[props[i]] = values[i]; - } - return nObj; + var nObj = {}; + for(var i=0; i values.length -1) + nObj[props[i]] = undefined; + + nObj[props[i]] = values[i]; + } + return nObj; } function includes(a, val, idx=0){ - if (typeof a === 'string') { - if(a.indexOf(val) !== -1) - return true; - } - else if (Array.isArray(a)) { - if (idx > 0) { - var nArr = a.slice(idx); - if(nArr.indexOf(val) > -1) - return true; - } - else { - if(a.indexOf(val) > -1) - return true; - } - } - else if (typeof a === 'object') { - if (Object.values(a).indexOf(val) > -1) - return true; - } - else if (Array.isArray(a)) { - if (idx > 0) { - var nArr = a.slice(idx); - if(nArr.indexOf(val) > -1) - return true; - } - else - if(a.indexOf(val) > -1) - return true; - } - - return false; + if (typeof a === 'string') { + if(a.indexOf(val) !== -1) + return true; + } + else if (Array.isArray(a)) { + if (idx > 0) { + var nArr = a.slice(idx); + if(nArr.indexOf(val) > -1) + return true; + } + else { + if(a.indexOf(val) > -1) + return true; + } + } + else if (typeof a === 'object') { + if (Object.values(a).indexOf(val) > -1) + return true; + } + else if (Array.isArray(a)) { + if (idx > 0) { + var nArr = a.slice(idx); + if(nArr.indexOf(val) > -1) + return true; + } + else + if(a.indexOf(val) > -1) + return true; + } + + return false; } function sample(arr){ - var num = Math.floor(Math.random() * arr.length); - return arr[num]; + var num = Math.floor(Math.random() * arr.length); + return arr[num]; } function cloneDeep(a){ - if (Array.isArray(a)) { - var nArr = []; - for (var i=0; i < a.length; i++) { - if (Array.isArray(a[i]) || typeof a[i] === 'object') - nArr.push(cloneDeep(a[i])); - else - nArr.push(a[i]); - } - - return nArr; - } - else if (typeof a === 'object') { - var nObj = {}; - for (key in a) { - if ( Array.isArray(a[key]) || typeof a[key] === 'object') - nObj[key] = cloneDeep(a[key]); - } - return nObj; - } + if (Array.isArray(a)) { + var nArr = []; + for (var i=0; i < a.length; i++) { + if (Array.isArray(a[i]) || typeof a[i] === 'object') + nArr.push(cloneDeep(a[i])); + else + nArr.push(a[i]); + } + + return nArr; + } + else if (typeof a === 'object') { + var nObj = {}; + for (key in a) { + if ( Array.isArray(a[key]) || typeof a[key] === 'object') + nObj[key] = cloneDeep(a[key]); + } + return nObj; + } } function sumBy(arr, second){ - var sum = 0; - if(typeof second === 'function') { - for(var i=0; i end && num < start; - else - return num < end && num > start; + + if (start != 0 && end === undefined) { + end = start + start = 0; + } + + if (num < 0) + return num > end && num < start; + else + return num < end && num > start; } function has(obj, gKeys){ - if(typeof gKeys === 'string') - gKeys = gKeys.split('.'); - - var presentKeys = []; - function helper(a) { - for (key in a) { - presentKeys.push(key); - - if(typeof a[key] === 'object') - helper(a[key]); - } - } - helper(obj); - - for(var i=0; i= len) + return str; + if(chars === undefined) { + chars = ""; + for (var i=0; i Date: Thu, 10 Aug 2017 19:49:00 -0700 Subject: [PATCH 16/17] prototype exercise sol --- prototypes_exercise/prototypes.js | 71 +++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/prototypes_exercise/prototypes.js b/prototypes_exercise/prototypes.js index e69de29b..d26971cc 100644 --- a/prototypes_exercise/prototypes.js +++ b/prototypes_exercise/prototypes.js @@ -0,0 +1,71 @@ +function Person(firstName, lastName, favoriteColor, favoriteNumber, favoriteFoods, family) { + this.firstName = firstName; + this.lastName = lastName; + this.favoriteColor = favoriteColor; + this.favoriteNumber = favoriteNumber; + this.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(p1) { + if (p1 instanceof Person) { + if(!this.family.includes(p1)) { + this.family.push(p1); + } + return this.family; + } +} + + +Array.prototype.map = function(cb) { + var nArr = []; + for(var i=0; i< this.length; i++ ) { + nArr.push(cb(this[i], i, this)); + } + + return nArr; +} + +Array.prototype.reduce = function(cb) { + var args = [].slice.call(arguments); + if(arguments.length === 1) { + var retValue = this[0]; + for(var i=1; i< this.length; i++) { + retValue = cb(retValue, this[i], i, this); + } + return retValue; + } + else { + for(var i=0; i< this.length; i++) { + args[1] = cb(args[1], this[i], i, this); + } + return args[1]; + } +} + +String.prototype.reverse = function() { + var rStr = ""; + + for(var i=0; i< this.length; i++) { + rStr = this[i] + rStr; + } + + return rStr; +} + + +Function.prototype.bind = function(thisArg, ...oArgs) { + var _this = this; + return function(thisArg) { + return _this.apply(thisArg, [...arguments, ...oArgs]); + } +} \ No newline at end of file From 3e0dffec942fa14ed71a47374068c7bb123752c7 Mon Sep 17 00:00:00 2001 From: Chitwant Date: Mon, 14 Aug 2017 11:06:04 -0700 Subject: [PATCH 17/17] assesment hack-snooze partial sol --- ajax_with_jquery_exercise/app.css | 92 ++++++++++++++++ ajax_with_jquery_exercise/app.js | 168 ++++++++++++++++++++++++++++++ 2 files changed, 260 insertions(+) create mode 100644 ajax_with_jquery_exercise/app.css create mode 100644 ajax_with_jquery_exercise/app.js diff --git a/ajax_with_jquery_exercise/app.css b/ajax_with_jquery_exercise/app.css new file mode 100644 index 00000000..e476c43a --- /dev/null +++ b/ajax_with_jquery_exercise/app.css @@ -0,0 +1,92 @@ +.row { + background-color: #f6f6ef; + } + + .borderless td, .borderless th { + border: none; + } + .navbar { + background-color: #ff6600; + margin-bottom: 0; + } + .navbar-text p { + display: inline; + } + + /*#loggedUser { + display:inline-block; !important + vertical-align: middle; !important + }*/ + + #loggedUser span { + display: block; + } + + +.formdeco { + background-color: #f6f6ef; + padding-top: 10px; +} +.noShow { + /*padding-bottom: 0px;*/ + display: none; + } + + .loginOnly { + display: none; + } + + li { + padding: 5px; + } + ol { + padding-top: 0px; + background-color: #f6f6ef; + /*text-decoration: none !important; + color: black !important;*/ + + } + section + ol { + margin-top: -20px; +} +ol { + padding-top: 20px; +} + +.navbar-text span:hover { + cursor: pointer; + text-decoration: underline; +} + +.navbar-right li { + padding-left: 0px; + /*margin-right: 10px;*/ +} + +.navbar ul { + margin-right: 0px; +} + +ol a:link { + text-decoration: none !important; + /*color: inherit;*/ +} + +ol a { + color: black !important; +} + +span.cuthead { + font: 8pt Verdana, Geneva, sans-serif !important; + /*font: italic small-caps normal 13px/150% Arial, Helvetica, sans-serif; + font-size: 8pt !important; + font-family: Verdana, Geneva, sans-serif !important;*/ + /*color: #828282;*/ + +} + +span.cuthead:hover { + cursor: pointer; + display: inline-block; + text-decoration: underline; +} \ No newline at end of file diff --git a/ajax_with_jquery_exercise/app.js b/ajax_with_jquery_exercise/app.js new file mode 100644 index 00000000..12cd92d7 --- /dev/null +++ b/ajax_with_jquery_exercise/app.js @@ -0,0 +1,168 @@ +$(document).ready(function() { + + // do this + fetchTopTwentyStories(); + + var signUp; + + +$('#login').on('click', function(event) { + if ($( this ).text() === "logout") { + var email = $( ".navbar-right li" ).eq(1).text(); + // remove text on right + $( ".navbar-right li" ).each(function() { + $( this ).remove(); + }); + + // toggle favorite + $('.navbar-text text').eq(1).toggleClass('loginOnly'); + $('#fav').toggleClass('loginOnly'); + //$('.loginOnly').toggleClass('loginOnly'); + + // remove auth token from local storage + localStorage.removeItem(email); + + // change text back to login + $( this ).text("login").wrapInner(""); + + // empty stories list display + $("ol").empty(); + } + else { + $('section').toggleClass('noShow'); + signUp = false; + } +}); + +$('#signup').on('click', function(event) { + $('section').toggleClass('noShow'); + signUp = true; +}); + +$('.form-horizontal').on('submit', function(event) { + if(signUp) { + loginOrSignUp("signup"); + } + else { + loginOrSignUp("login"); + } + + $('section').toggleClass('noShow'); + event.preventDefault(); +}); + +$('#fav').on("click", function(e) { + if ($( this ).text() === "all") { + $( "li span" ).each(function( index ) { + if($( this ).hasClass('glyphicon-star-empty') ) { + $( this ).parent().show(); + } + }); + $( this ).text("favorites").wrapInner(""); + } + else if ( $( this ).text() === "favorites" ) { + $( "li span" ).each(function( index ) { + if($( this ).hasClass('glyphicon-star-empty') ) { + $( this ).parent().hide(); + } + }); + $( this ).text("all").wrapInner(""); + + } + + }); + + $("ol").on("click", ".glyphicon", function(e) { + if( $(e.target).hasClass('glyphicon-star-empty') ) { + + $(e.target).toggleClass('glyphicon-star-empty glyphicon-star'); + } + else { + $(e.target).toggleClass('glyphicon-star glyphicon-star-empty'); + } + }); +}); + +function fetchTopTwentyStories() { + $.get("https://hacker-news.firebaseio.com/v0/topstories.json") + .then(function(stories) { + return stories.slice(0, 20); + }) + .then(function(tStories) { + tStories.forEach(function(element) { + $.get('https://hacker-news.firebaseio.com/v0/item/' + element + '.json') + .then(function(storyDetail) { + if ('url' in storyDetail) { + var cutUrl = ""; + var iUrl = storyDetail.url; + if (iUrl.includes("www.")) { + cutUrl = iUrl.split('www.')[1].split('/')[0].split('.').slice(-2).join('.'); + } + else { + cutUrl = storyDetail.url.split('//')[1].split('/')[0]; + if (cutUrl.match(/(.*\.)+(.*\.)+.*/g)) { + cutUrl = cutUrl.split('.').slice(-2).join('.'); + } + } + var element = '
  • ' + storyDetail.title + ' (' + + '' + cutUrl + + ')
  • '; + $('ol').append(element); + } + }) + .fail(function(error) { + console.warn("Isuue encountered in item fetch api"); + }); + }); + }) + .fail(function(error) { + console.warn("Isuue encountered in top stories fetch api"); + }); +} + +function loginOrSignUp(str) { + var email = $('#email').val(); + var password = $('#password').val(); + $.ajax({ + method: "POST", + headers: { + "Content-Type": "application/json" + }, + + url: 'https://hn-favorites.herokuapp.com/' + str + '', + data: JSON.stringify({ + email: email, + password: password + }) + }) + .then(function(data) { + if ( data.auth_token.length > 0 && data.auth_token.includes('.') ) { + alert(str + " successful."); + // save the auth token in local storage + localStorage.setItem(email, data.auth_token); + $('.form-horizontal').each(function() { + this.reset(); + }); + + // toggle login to logout and add text logged in as on right nav + // and show the favorites tab/icon + if(str === "login") { + $('.navbar-text text').eq(1).toggleClass('loginOnly'); + $('#fav').toggleClass('loginOnly'); + //$('.loginOnly').toggleClass('loginOnly'); + var element = '
  • Logged in as:
  • ' + + '
  • ' + email + '
  • '; + $('.navbar-right').append(element); + $('#login').text("logout").wrapInner(""); + } + } + }) + .fail(function(err) { + if( 'status' in err && 'statusText' in err ) + // if( err.status !== undefined && err.statusText !== undefined ) + alert(str + " not successful. " + err.status + ": " + err.statusText); + else + alert(str + " not successful."); + }); +} \ No newline at end of file