Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions ajax_with_jquery_exercise/app.css
Original file line number Diff line number Diff line change
@@ -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;
}
168 changes: 168 additions & 0 deletions ajax_with_jquery_exercise/app.js
Original file line number Diff line number Diff line change
@@ -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("<strong />");

// 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("<strong />");
}
else if ( $( this ).text() === "favorites" ) {
$( "li span" ).each(function( index ) {
if($( this ).hasClass('glyphicon-star-empty') ) {
$( this ).parent().hide();
}
});
$( this ).text("all").wrapInner("<strong />");

}

});

$("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 = '<li><span class="glyphicon glyphicon-star-empty' +
'"></span> <a href="' + storyDetail.url + '"> ' + storyDetail.title + '</a> (' +
'<span class="cuthead"><a href="from?site=' + cutUrl + '"><span>' + cutUrl +
'</span></a></span>)</li>';
$('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 = '<li><b>Logged in as:</b></li>' +
'<li><b>' + email + '</b></li>';
$('.navbar-right').append(element);
$('#login').text("logout").wrapInner("<strong />");
}
}
})
.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.");
});
}
68 changes: 68 additions & 0 deletions call_apply_bind_exercise/callApplyBind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var obj = {
fullName: "Harry Potter",
person: {
sayHi: function(){
return "This person's name is " + this.fullName
}.bind(obj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work because obj won't be defined yet. Here is a work around:

var obj = {
  fullName: "Harry Potter",
  person: {
    sayHi() {
      return `This person's name is ${this.fullName}`;
    }
  }
}
obj.person.sayHi = obj.person.sayHi.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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you count down, you don't even need to copy the variable. Numbers are not reference types.

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!";
}
}
}
Loading