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
93 changes: 93 additions & 0 deletions ajax_with_jquery_exercise/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hacker News Clone</title>
<!-- Link to stylesheet -->
<link rel="stylesheet" type="text/css" href="style.css">

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- jQuery script link -->
<!-- jQuery should be included before JavaScript script -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<!-- script to main.js -->
<script src="main.js"></script>
</head>

<body>
<div class="container">
<div class="row">

<div class="col col-md-10" style="height: 100px; background-color: #e65c00;">
<h3>Hack or Snooze</h3>
</div>
<div class="col col-md-1" style="height: 100px; background-color: yellow;">
<a href="#login" data-toggle="collapse">Log In</a>
</div>
<div class="col col-md-1" style="height: 100px; background-color: lightgray;">
<a href="#login" data-toggle="collapse">Sign Up</a>
</div>


</div>

<form action="">
<div class="row collapse" id='login'>
<!-- <div class="col col-md-12" style="height: 200px; background-color: yellow;"> -->

<div class="form-group row">
<label for="email" class="col-md-2 col-form-label">Email</label>
<div class="col-md-12">
<input class="form-control" type="email" value="bootstrap@example.com" id="email">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-2 col-form-label">Title</label>
<div class="col-md-6">
<input class="form-control" type="text" value="Input new title..." id="password">
</div>


</div>

<div class="form-group row">
<button type="submit" class="btn btn-primary">Submit</button>
</div>

<!-- </div> -->
</div>
<div class="row">
<div class="col col-md-12" id="top20" style="height: 200px; background-color: #fff0e6;">
<!-- <h3>Ordered List</h3> -->
<ol>
</ol>
</div>
</div>
</form>

</div>



</body>
</html>













167 changes: 167 additions & 0 deletions ajax_with_jquery_exercise/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
$(function() {

$("form").on("submit", function(e) {
e.preventDefault();

topStories()
signUpUser()

// var $newTitle = $('#newTitle').val()
// var $newURL = $('#urlToAdd').val()
// var icon = "<span class=\"glyphicon glyphicon-star-empty\" aria-hidden=\"true\"></span>"
// var $newLi = $("<li>" + icon + " " + $newTitle + " " + $newURL + "</li>");
// $("ol").append($newLi);

// $newTitle.val("");
// $newURL.val("");
});


$('ol').click(function(event){
$(event.target).toggleClass('glyphicon-star-empty glyphicon-star');

addToFavorites()


});

$("btnFavs").on("click", function(e) {
e.preventDefault();

addToFavorites();
retrieveFavorites();

} )


//-----------------------------------------------------------------------
// Create the Top Stories List
var icon = "<span class=\"glyphicon glyphicon-star-empty\" aria-hidden=\"true\"></span>"

function topStories() {
$.get( "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty", function( data ) {
var arrIDs = [];
//create array of Top 20 story IDs, that push a complete url to the array
for (var i = 0; i < 20; i++) {
arrIDs.push("https://hacker-news.firebaseio.com/v0/item/" + data[i] + ".json")
}
//use the url's from above to run another get funtion to get the details for each story ID
for (var i = 0; i < arrIDs.length; i++) {
$.get(arrIDs[i])
.then(function (data) {
// $('ol').append("<li>" + icon + "<h5>" + data.title + "</h5> (" + data.url + ") </li>")
$('ol').append("<li>" + icon + "<h5>" + data.title + "</h5> </li>")

})
.fail(function(err) {
console.log(err);
});
}
})
}



var $email = $('#email').val();
// console.log($email);
var $password = $('#password').val();
// console.log($password);
var authKy = "";


function signUpUser(email, password) {

$.ajax({
method: "POST",
headers: {
"Content-Type": "application/json"
},
url: "https://hn-favorites.herokuapp.com/signup",
data: JSON.stringify({
email: $email,
password: $password
})
})
.then(function(data) { authKy = (data.auth_token) })
.fail(err => console.warn(err))

}

function logInUser(email, password) {
$.ajax({
method: "POST",
headers: {
"Content-Type": "application/json"
},
url: "https://hn-favorites.herokuapp.com/signup",
data: JSON.stringify({
email: $email,
password: $password
})
})
.then(function(data) { authKy = (data.auth_token)})
.fail(err => console.warn(err))
}

//If a story is clicked, do addToFavorites() function that will add it to the favorites list
function addToFavorites() {
$.ajax({
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": authKy
},
url: "https://hn-favorites.herokuapp.com/stories.json",
data: JSON.stringify({
hacker_news_story: {
by: "aj",
story_id: "123",
title: "Test story",
url: "abc.com"
}
})
})
.then(function(data) { console.log(data) })
.fail(err => console.warn(err))
}

function retrieveFavorites() {
$.ajax({
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": authKy
},
url: "https://hn-favorites.herokuapp.com/stories.json",
data: JSON.stringify({
hacker_news_story: {
by: "aj",
story_id: "123",
title: "Test story",
url: "abc.com"
}
})
})
.then(function(data) { console.log("fav's response: " + data) })
.fail(err => console.warn(err))
}



});
















66 changes: 66 additions & 0 deletions call_apply_bind_exercise/callApplyBind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
function sumEvenArguments() {
var result = 0;
for(var i = 0; i < arguments.length; i++) {
if(arguments[i] % 2 === 0) {
result += arguments[i];
}
}
return result
}


function arrayFrom() {
var finalArr = [];
for(var i = 0; i < arguments.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can skip the new array and the loop by using slice:

  return Array.prototype.slice.apply(arguments);

finalArr.push(arguments[i]);
}
return finalArr;
}

function invokeMax(fn, maxAmount) {

var counter = 0;

return function() {
counter++;

if(counter > maxAmount) {
return "Maxed Out!"
} else {
return fn.apply(this, [].slice.call(arguments))
}
}
}


var addOnlyThreeTimes = invokeMax(add,3);
addOnlyThreeTimes(1,2); // 3
addOnlyThreeTimes(2,2); // 4
addOnlyThreeTimes(1,2); // 3
addOnlyThreeTimes(1,2); // "Maxed Out!"



function guessingGame(amount) {

var answer = Math.round(Math.random()*10)
var guesses = 0;


return function(guess) {
if(guesses < amount) {
guesses++
} else {
return "Game Over!"
}

if(guess === answer){
return "You got it!";
} else if(guess > answer) {
return "You're too high!";
} else if(guess < answer) {
return "You're too low!"
}

}
}
10 changes: 4 additions & 6 deletions call_apply_bind_exercise/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ Fix the following code:
```javascript
var obj = {
fullName: "Harry Potter",
person: {
sayHi: function(){
return "This person's name is " + this.fullName
}
sayHi: function(){
return "This person's name is " + this.fullName;
}
}
```

- List two examples of "array-like-objects" that we have seen.
-
-
- arguments
- sets

### Functions to write:

Expand Down
Binary file added canvas_exercise/.DS_Store
Binary file not shown.
Loading