diff --git a/ajax_with_jquery_exercise/hack_clone.html b/ajax_with_jquery_exercise/hack_clone.html new file mode 100644 index 00000000..fb2e68b6 --- /dev/null +++ b/ajax_with_jquery_exercise/hack_clone.html @@ -0,0 +1,60 @@ + + + + + + + Hacker News + + + + + + + +
+ + + + + + + +
+
+
    +
+
+
+ +
+ + + + diff --git a/ajax_with_jquery_exercise/hack_clone.js b/ajax_with_jquery_exercise/hack_clone.js new file mode 100644 index 00000000..fc698474 --- /dev/null +++ b/ajax_with_jquery_exercise/hack_clone.js @@ -0,0 +1,37 @@ +$(function() { + + var $feed = $("#feed"); + //Uses the Hacker News API to display top stories (up to some limit, say 10 or 20). + + //get item id from responseJSON + // var $topArray = []; + // var $myObj = $.get("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"); + // + // for(var i = 0; i < 25; i++) { + // $topArray.push($myObj.responseJSON[i]); + // } + //Stories, comments, jobs, Ask HNs and even polls are just items. + //They're identified by their ids, which are unique integers, and live under /v0/item/. + //$.get("https://hacker-news.firebaseio.com/v0/item/14962634.json") + + $.get("https://hacker-news.firebaseio.com/v0/topstories.json") + .then(function(obj){ + var items = obj.slice(0, 25).map(function(current) { + return $.get(`https://hacker-news.firebaseio.com/v0/item/${current}.json`); + }); + return Promise.all(items); + }) + .then(function(news) { + news.forEach(function(item){ + $feed.append(`
  • ${item.title}
  • `); + }); + }); + + //The top stories page should have a button to log in or sign up for an account. HTML + + //When the user logs in or signs up succesfully, a token sent back from the server should get stored in localStorage. + // This token will be used to authenticate the user on subsequent requests to the stories API. + // (Examples of requests to this API are below.) + + +}); diff --git a/call_apply_bind_exercise/callApplyBind.js b/call_apply_bind_exercise/callApplyBind.js index e69de29b..970ad4a8 100644 --- a/call_apply_bind_exercise/callApplyBind.js +++ b/call_apply_bind_exercise/callApplyBind.js @@ -0,0 +1,46 @@ +var obj = { + fullName: "Harry Potter", + sayHi(){ + return `This person's name is ${this.fullName}`; + } +} + +//array like objects +//arrays from DOM methods +//arguments + +function sumEvenArguments(){ + var args = [].slice.call(arguments); + return args.reduce(function (all, item){ + if (item % 2 === 0) return all + item; + return all; + },0); +} + +function arrayFrom() { + var args = [].slice.call(arguments); + return args; +} + +function invokeMax(fn, max) { + var count = 0; + return function(){ + var arg = [].slice.call(arguments); + count++; + if (count > max) return `Maxed Out!`; + + return fn.apply(this, arg); + } +} + +function guessingGame(amount) { + var answer = Math.floor(Math.random() * 10); + var guesses = 0; + return function(guess) { + guesses++; + if (guesses > amount) return `No more guesses the answer was ${answer}`; + if (guess === answer) return "You got it!"; + if (guess < answer) return "You're too low!"; + if (guess > answer) return "You're too high!" + } +} diff --git a/canvas_exercise/shapes_game/index.js b/canvas_exercise/shapes_game/index.js index 0de5f18a..7ec74d8e 100644 --- a/canvas_exercise/shapes_game/index.js +++ b/canvas_exercise/shapes_game/index.js @@ -1,15 +1,60 @@ window.addEventListener("load", function() { - function clear(ctx, width, heigt) { + function clear(ctx, width, height) { + ctx.clearRect(0, 0, width, height); } - function drawRandomShape(ctx, width, height) { + function drawRandomShape(ctx) { + //0 = white triangle 1 = red triangle 2 = white square 3 = red square + var shapes = [0, 1, 2, 3]; + var x = Math.floor(Math.random() * (650 - 100) + 100); + var y = Math.floor(Math.random() * (650 - 100) + 100); + + //{white0: 38, red1: 40, red0: 37, white1: 39} + if (shapes.indexOf(Math.floor(Math.random() * (4 - 0))) === 0) { + ctx.fillStyle = 'white'; + ctx.beginPath(); + ctx.moveTo(x, x); + ctx.lineTo(x, x + 70); + ctx.lineTo(x + 70, x + 70); + ctx.fill(); + ctx.closePath(); + return 'white0'; + } + else if (shapes.indexOf(Math.floor(Math.random() * (4 - 0))) === 1) { + ctx.fillStyle = 'red'; + ctx.beginPath(); + ctx.moveTo(x, x); + ctx.lineTo(x, x + 70); + ctx.lineTo(x + 70, x + 70); + ctx.fill(); + ctx.closePath(); + return 'red0'; + } + else if (shapes.indexOf(Math.floor(Math.random() * (4 - 0))) === 2) { + ctx.fillStyle = 'white'; + ctx.fillRect(x, y, 70, 70); + return 'white1'; + } + else { + ctx.fillStyle = 'red'; + ctx.fillRect(x, y, 70, 70); + return 'red1'; + } } function drawGameStartText(ctx, width, height, score) { + ctx.fillStyle = 'white'; + ctx.font = '36px serif'; + ctx.fillText('Press the space bar to start a new game', width, height); + if (score !== undefined) { + ctx.fillText('Score: ' + score, width + 225, height + 50); + } } function restartGame(ctx, width, height) { + countdown = 30; + scoreSpan.innerHTML = 0; } var canvas = document.getElementById("shapes-game"), @@ -23,14 +68,46 @@ window.addEventListener("load", function() { expectedKeysMap = {white0: 38, red1: 40, red0: 37, white1: 39}, timerSpan = document.getElementById("time-remaining"), scoreSpan = document.getElementById("score-val"), - seconds = 3, + countdown = 30, intervalId; canvas.width = width; canvas.height = height; - document.addEventListener("keyup", function() { - + document.addEventListener("keyup", function(event) { + if (!gameOn && event.keyCode === 32) { + gameOn = true; + clear(ctx, width, height); + expectedKey = expectedKeysMap[drawRandomShape(ctx)]; + intervalId = setInterval(function() { + countdown--; + if (countdown === 0) { + clearInterval(intervalId); + gameOn = false; + var endScore = +scoreSpan.innerHTML; + restartGame(); + clear(ctx, width, height); + drawGameStartText(ctx, 100, 400, endScore); + } + timerSpan.innerHTML = countdown; + }, 1000); + //0 = white triangle (up) 1 = red triangle(left) 2 = white square(right) 3 = red square(down) + } + else if (gameOn && Object.values(expectedKeysMap).includes(event.keyCode)) { + if (expectedKey === event.keyCode) { + scoreSpan.innerHTML = Number(scoreSpan.innerHTML) + 1; + } + else { + scoreSpan.innerHTML = Number(scoreSpan.innerHTML) - 1; + } + clear(ctx, width, height); + expectedKey = expectedKeysMap[drawRandomShape(ctx)]; + } }); -}); + drawGameStartText(ctx, 100, 400); + //once key gets pressed it runs functions attached + //waits for next key to be pressed + // + //match expectedKey to expectedKeysMap +}); diff --git a/jquery_exercise/hack_clone.html b/jquery_exercise/hack_clone.html new file mode 100644 index 00000000..c3bc1f01 --- /dev/null +++ b/jquery_exercise/hack_clone.html @@ -0,0 +1,46 @@ + + + + + + + Hacker News + + + + + + + + +
    +
    + title:
    +
    + url:
    + + +
    +
      +
    1. test site
    2. +
    +
    + + + + diff --git a/jquery_exercise/hack_clone.js b/jquery_exercise/hack_clone.js new file mode 100644 index 00000000..e741fc52 --- /dev/null +++ b/jquery_exercise/hack_clone.js @@ -0,0 +1,46 @@ +$(function() { + var $sites = $("#sites"); + $("#news").hide(); + + $("form").on("submit", function(e) { + e.preventDefault(); + + var $title = $("#title"); + var titleText = $title.val(); + var $url = $("#url"); + var urlText = $url.val(); + // var $newStar = $("", { + // class: "glyphicon glyphicon-star-empty", + // ariaHidden: true + // }); + var $newLi = $("
  • ", { + + }); + $newLi.html(` + ${titleText} + `); + + // $sites.prepend($newStar); + $sites.append($newLi); + $title.val(""); + $url.val(""); + $("#news").hide(); + }); + $("#nav-submit").on("click", function() { + $("#news").show(); + }); + // + $(".glyphicon").on("click", function(e) { + if ($(".glyphicon").hasClass("glyphicon-star-empty")) { + $(".glyphicon").removeClass("glyphicon-star-empty").addClass("glyphicon-star"); + } + else { + $(".glyphicon").removeClass("glyphicon-star").addClass("glyphicon-star-empty"); + } + }); + //make it look nice + // + //favorites on click hide empty stars + // + +}); diff --git a/lodash_exercise/lodash.js b/lodash_exercise/lodash.js index 483d734a..c39ac4a2 100644 --- a/lodash_exercise/lodash.js +++ b/lodash_exercise/lodash.js @@ -1,85 +1,210 @@ -function drop(){ - +function drop(arr, num){ + if (num === 0) return arr; + if (!num) num = 1; + return arr.slice(num); } -function fromPairs(){ - +function fromPairs(arr){ + var newObj = {}; + for (var i = 0; i < arr.length; i++) { + newObj[arr[i][0]] = arr[i][1]; + } + return newObj; } -function head(){ - +function head(arr){ + return arr.shift(); } -function take(){ - +function take(arr, num){ + if (num === 0) return []; + if (!num) return [arr[0]]; + return arr.slice(0, num); } -function takeRight(){ - +function takeRight(arr, num){ + if (num === 0) return []; + if (!num) return [arr.length]; + if (num > arr.length -1) return arr; + return arr.slice(num - 1, arr.length); } function union(){ - -} - -function zipObject(){ - -} - -function includes(){ - -} - -function sample(){ - -} - -function cloneDeep(){ - -} - -function sumBy(){ - -} - -function inRange(){ - -} - -function has(){ - -} - -function omit(){ - -} - -function pick(){ - -} - -function pickBy(){ - -} - -function omitBy(){ - -} - -function padEnd(){ - -} - -function repeat(){ - + var array = []; + for (var i = 0; i < arguments.length; i++) { + array = array.concat(arguments[i]); + } + return array.filter(function(val, index, arr) { + return arr.indexOf(val) === index; + }); +} + +function zipObject(arr1, arr2){ + var myObj = {}; + for (var i = 0; i < arr1.length; i++) { + myObj[arr1[i]] = arr2[i]; + } + return myObj; +} + +function includes(collection, src){ + if (arguments.length > 2) return false; + if (typeof collection === 'string') { + if (collection.match(src)) return true; + return false; + } + if (Array.isArray(collection)) { + for (var i = 0; i < collection.length; i++) { + if (collection[i] === src) return true; + else return false; + } + } + if (typeof collection === 'object') { + for (var key in collection) { + if (collection[key] === src) return true; + return false; + } + } +} + +function sample(arr){ + return arr[Math.floor(Math.random() * arr.length)]; +} + +function cloneDeep(val){ + if (Array.isArray(val)) { + var newVal = []; + for (var i = 0; i < val.length; i++){ + newVal = newVal.concat(cloneDeep(val[i])); + } + return newVal; + } + else { + var newVal = {}; + for (var key in val) { + newVal[key] = val[key]; + } + return newVal; + } +} + +function sumBy(arr, key){ + return arr.reduce(function(all, item, index){ + if (typeof key === 'function') { + return all + key(item); + } + return all + item[key]; + }, 0); +} + +function inRange(num, start, end){ + if (start > end) { + if (num > end & num < start) return true; + } + if (typeof end === 'undefined') { + end = start; + start = 0; + } + if (num > start && num < end) return true; + else return false; +} + +function has(obj, path){ + var isTrue = true; + if (Array.isArray(path)) { + for (var i = 0; i < path.length; i++) { + for (var key in obj) { + if (obj[key] === path[i]) isTrue && true; + else isTrue && false; + } + } + } + else { + for (var key in obj) { + if (obj[key] === path) isTrue && true; + else isTrue && false; + } + } + return isTrue; +} + +function omit(obj, path){ + var newObj = {}; + for (var key in obj) { + if (path.indexOf(key) === -1) { + newObj[key] = obj[key]; + } + } + return newObj; +} + +function pick(obj, path){ + var newObj = {}; + for (var key in obj) { + if (path.indexOf(key) !== -1) { + newObj[key] = obj[key]; + } + } + return newObj; +} + +function pickBy(obj, fn){ + var newObj = {}; + for (var key in obj) { + if (fn(obj[key])) { + newObj[key] = obj[key]; + } + } + return newObj; +} + +function omitBy(obj, fn){ + var newObj = {}; + for (var key in obj) { + if (!fn(obj[key])) { + newObj[key] = obj[key]; + } + } + return newObj; +} + +function padEnd(str, length, char){ + var newStr = ''; + if (str.length >= length) return str; + if (char === undefined) { + for (var i = 0; i < length - str.length; i++) { + newStr += ' '; + } + return str.concat(newStr); + } + for (var i = 0; i < length - str.length; i++) { + newStr += char[i % char.length]; + } + return str.concat(newStr); +} + +function repeat(str, num){ + var newStr = ''; + if (num === 0) return newStr; + for (var i = 0; i < num; i++) { + newStr = newStr.concat(str); + } + return newStr; } function upperFirst(str){ - -} - -function flatten(){ - + return str[0].toUpperCase().concat(str.slice(1)); +} + +function flatten(arr){ + var newArr = []; + for (var i = 0; i < arr.length; i++) { + if (Array.isArray(arr[i])) { + newArr = newArr.concat(arr[i]); + } + else newArr.push(arr[i]); + } + return newArr; } function zip(){ diff --git a/recursion_exercise/recursion.js b/recursion_exercise/recursion.js index e69de29b..b235093e 100644 --- a/recursion_exercise/recursion.js +++ b/recursion_exercise/recursion.js @@ -0,0 +1,34 @@ +function productOfArray(arr) { + if (arr.length === 0) return 1; + return arr[0] * productOfArray(arr.splice(1)); +} + +function collectStrings(obj) { + var newArray = []; + function helper(myObj) { + for (var key in myObj) { + if (typeof myObj[key] === 'object') { + helper(myObj[key]); + } + else { + newArray.push(myObj[key]); + } + } + } + helper(obj); + return newArray; +} + +function contains(obj, src) { + var isFound = false; + for (var key in obj) { + if (typeof obj[key] === 'object') { + isFound = contains(obj[key], src); + } + else if (obj[key] === src) { + return true; + } + if (isFound) return true; + } + return false; +} diff --git a/testing_exercise/testing.js b/testing_exercise/testing.js index e69de29b..0687d95a 100644 --- a/testing_exercise/testing.js +++ b/testing_exercise/testing.js @@ -0,0 +1,41 @@ +function replaceWith(str, char, replaced) { + return str.split(char).join(replaced); +} + +function expand(array, num) { + var newArray = []; + for (var i = 0; i < num; i++) { + for(var j = 0; j < array.length; j++) { + newArray.push(array[j]); + } + } + return newArray; +} + +function acceptNumbersOnly() { + var isTrue = true; + for (var i = 0; i < arguments.length; i++) { + if (arguments[i] === parseInt(arguments[i])) { + isTrue = isTrue && true; + } + else isTrue = false; + + } + return isTrue; +} + +function mergeArrays(array1, array2) { + var newArray = array1.concat(array2); + return newArray.sort(function(a, b) { + return a - b; + }); +} + +function mergeObjects(obj1, obj2) { + for (var key in obj2) { + if (obj2.hasOwnProperty(key)){ + obj1[key] = obj2[key]; + } + } + return obj1; +} diff --git a/testing_exercise/testingSpec.js b/testing_exercise/testingSpec.js index aef56b1d..e91532d0 100644 --- a/testing_exercise/testingSpec.js +++ b/testing_exercise/testingSpec.js @@ -1,3 +1,54 @@ var expect = chai.expect; -// WRITE YOUR TESTS HERE! \ No newline at end of file +describe('replaceWith', function(){ + it('Basic function replace letters', function() { + expect(replaceWith('awesome', 'e', 'z')).to.equal('awzsomz'); + }); + it('Case sensitive', function() { + expect(replaceWith('Foo', 'F', 'B')).to.equal('Boo'); + }); +}); + +describe('expand', function() { + it('Basic function with numbers', function(){ + expect(expand([1, 2, 3], 3)).to.deep.equal([1, 2, 3, 1, 2, 3, 1, 2, 3]); + }); + + it('Basic function with strings', function(){ + expect(expand(['foo', 'test'], 1)).to.deep.equal(['foo', 'test']); + }); +}); + +describe('acceptNumbersOnly', function() { + it('Basic function', function() { + expect(acceptNumbersOnly(1, 'foo')).to.equal(false) + }); + + it('Basic function true', function() { + expect(acceptNumbersOnly(1, 2, 3, 4, 5)).to.equal(true); + }); + + it('Basic function NaN', function() { + expect(acceptNumbersOnly(1, 2, NaN, 4, 5)).to.equal(false); + }); +}); + +describe('mergeArrays', function() { + it('Basic function merges arrays', function() { + expect(mergeArrays([2, 1], [3, 4])).to.deep.equal([1, 2, 3, 4]); + }); +}); + +describe('mergeObjects', function() { + var obj1= { + name: "Foo", + num: 33 + } + var obj2 = { + test: "thing", + num: 55 + } + it('Basic function merge objects', function() { + expect(mergeObjects(obj1, obj2)).to.deep.equal({name: 'Foo', test: 'thing', num: 55}); + }); +});