diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 00000000..1034b25b
Binary files /dev/null and b/.DS_Store differ
diff --git a/ajax_with_jquery_exercise/index.css b/ajax_with_jquery_exercise/index.css
new file mode 100644
index 00000000..24445857
--- /dev/null
+++ b/ajax_with_jquery_exercise/index.css
@@ -0,0 +1,18 @@
+.row{
+ margin-bottom: 30px;
+}
+#btn_group{
+ width: 100%;
+ display: flex;
+ flex-direction:
+ row; justify-content: space-around;
+}
+form button{
+ width: 100px;
+}
+.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..29f30c20
--- /dev/null
+++ b/ajax_with_jquery_exercise/index.html
@@ -0,0 +1,50 @@
+
+
+
+
+ Linker
+
+
+
+
+
+
+
+
+
+
+ - This should be removed
+
+
+
+
+
+
+
+
\ 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..6b6b8817
--- /dev/null
+++ b/ajax_with_jquery_exercise/index.js
@@ -0,0 +1,203 @@
+$(document).ready(function(){
+ //variables
+ var $login_form = $(`#login-form`);
+ var $email = $(`#inp-email`);
+ var $pword = $(`#inp-password`);
+ var $login_fail = $(`#login-fail`);
+ var $login_link = $(`#login-link`);
+ var $links = $(`#links`);
+ var $fav = $(`#favor-link`);
+ var fav = [];
+ var all = [];
+
+ //imeadiat calls
+ if(!!localStorage.getItem('auth_token')) getFav();
+ getTop().then(function(data){
+ console.log(data);
+ all = data;
+ displayAll(data);
+ });
+
+ //click functions
+ $fav.click(function(){
+ console.log('fav click');
+ if(!localStorage.getItem('auth_token')){
+ $login_form .toggleClass("hidden");
+ }else if($fav.text() === `Favoriets`){
+ $fav.text('All');
+ displayFav(fav);
+ }else{
+ $fav.text('Favoriets');
+ displayAll(all);
+ }
+ });
+
+ $login_link.click(function(){
+ if($login_link.text() === `Logout`){
+ localStorage.removeItem('auth_token');
+ $login_link.text('Login');
+ fav = [];
+ }else{
+ $login_form .toggleClass("hidden");
+ }
+ });
+
+ $(`#login-btn`).click(function(e){
+ e.preventDefault();
+
+ console.log($email.val(), $pword.val());
+ getLogin($email.val(), $pword.val())
+ .then(function(token){
+ console.log(token)
+ $login_fail.addClass('hidden');
+ $login_form.addClass('hidden');
+ $login_link.text('Logout');
+ getFav();
+ })
+ .fail(function(){
+ $login_fail.removeClass('hidden');
+ })
+ });
+
+ $(`#signup-btn`).click(function(e){
+ e.preventDefault();
+
+ console.log($email.val(), $pword.val(), true);
+ getLogin($email.val(), $pword.val(), true)
+ .then(function(token){
+ console.log(token)
+ $login_fail.addClass('hidden');
+ $login_form.addClass('hidden');
+ $login_link.text('Logout');
+ })
+ .fail(function(){
+ $login_fail.removeClass('hidden');
+ })
+ });
+
+ $links.on('click', '.star', function(e){
+ var id = $(e.target).attr('storyid');
+ setFav(id);
+ });
+
+ // regular functions
+ function isFav(sid){
+ return fav.some(function(sid,v, v.story_id, sid == v.story_id){
+ console.log(v);
+ return sid === v.story_id;
+ });
+ }
+
+ function displayAll(arr){
+ $links.empty();
+ arr.forEach(function(v) {
+ displayLink(v.title,
+ v.url,
+ v.id,
+ isFav(v.id),
+ !!localStorage.getItem('auth_token'));
+ });
+ }
+
+ function displayFav(arr){
+ $links.empty();
+ arr.forEach(function(v) {
+ displayLink(v.title,
+ v.url,
+ v.storyId,
+ true,
+ true);
+ });
+ }
+
+ 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 storyFromID(id){
+
+
+ for(let s of all){
+ if(s.id == id) {
+ return s;
+ }
+ }
+ }
+
+ //ajax 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((err) => console.warn(err));
+ }
+
+ function getFav() {
+ return $.ajax({
+ method: "GET",
+ headers: {
+ "Authorization": localStorage.getItem('auth_token'),
+ },
+ 'url': 'https://hn-favorites.herokuapp.com/stories.json',
+ })
+ .then((favorites) => {console.log(favorites); return fav = favorites;})
+ .fail((err) => console.warn(err));
+ }
+
+ function getLogin(email, password, signup = false){
+ var urlEnd = 'login';
+ if(signup) urlEnd = 'signup';
+
+ return $.ajax({
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json"
+ },
+ url: `https://hn-favorites.herokuapp.com/${urlEnd}`,
+ data: JSON.stringify({
+ 'email': email,
+ 'password': password
+ })
+ }).then(function(data) {
+ localStorage.setItem('auth_token', data.auth_token);
+ auth_token = data.auth_token;
+ return(data.auth_token);
+ })
+ }
+
+ function setFav(id){
+ var s = storyFromID(id);
+
+ return $.ajax({
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "Authorization": localStorage.getItem('auth_token'),
+ },
+ url: `https://hn-favorites.herokuapp.com/stories.json`,
+ data: JSON.stringify({
+ "hacker_news_story": s,
+ })
+ })
+ .then((res) => console.log(res))
+ }
+
+});
\ No newline at end of file
diff --git a/call_apply_bind_exercise/callApplyBind.js b/call_apply_bind_exercise/callApplyBind.js
index e69de29b..fcc4817a 100644
--- a/call_apply_bind_exercise/callApplyBind.js
+++ b/call_apply_bind_exercise/callApplyBind.js
@@ -0,0 +1,50 @@
+"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);
+}
+
+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;
+ */
+}
+
+function invokeMax(fn, max){
+ var count = 0;
+
+ return function(){
+ if(++count > max) return `Maxed Out!`;
+ var arr = [].slice.call(arguments);
+
+ return fn.apply(this, arr);
+ };
+
+}
+
+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 36f0fedb..28cedae6 100644
--- a/call_apply_bind_exercise/readme.md
+++ b/call_apply_bind_exercise/readme.md
@@ -13,9 +13,40 @@ 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.
- -
- -
+ - arguments
+ - jQuery selectors like $('p');
### Functions to write:
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
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+