-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
128 lines (80 loc) · 4.17 KB
/
Copy pathcode.js
File metadata and controls
128 lines (80 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
$(document).ready(function(){ //when the document is ready we will do these things
$("#cityField").keyup(function(){
$("#txtHint").text($("#cityField").val());
$.getJSON("http://bioresearch.byu.edu/cs260/jquery/getcity.cgi?q=" + encodeURI(makeCaps($("#cityField").val())), function(data){
$("#txtHint").text(makeCaps($("#cityField").val()));
var everything = "<ul>";
$.each(data, function(i,item) {
everything += "<li> " + data[i].city;
});
everything += "</ul>";
$("#txtHint").html(everything);
});
});
$("#weatherButton").click(function(e){
console.log("Submit button pressed!");
e.preventDefault(); //will prevent the browswer from submitting the form
var value = $("#cityField").val();
var myUrl = "https://api.openweathermap.org/data/2.5/weather?q=" + encodeURI(value) + "&APPID=89d40a3deb8cccbf273764a68a681420&units=imperial";
$.ajax({
url: myUrl,
dataType: "json",
success: function(parsed_json) {
$("#displayCity").val(makeCaps(value));
var everything = "<ul>";
var weatherObj = parsed_json["weather"][0];
var description = weatherObj["description"];
var image = weatherObj["icon"];
var iconURL = "http://openweathermap.org/img/w/" + image + ".png";
$("#weatherIcon").attr("src", iconURL);
$("body").css('background-image', 'url(' + iconURL + ')');
var main = parsed_json["main"];
var temperature = main["temp"];
var humidity = main["humidity"];
var tempMax = main["temp_max"];
var tempMin = main["temp_min"];
var coord = parsed_json["coord"];
everything += "<li><h1>" + parsed_json["name"] + "</h1> lat: " + coord["lat"] + " long: " + coord["lon"] + "</li>";
everything += "<li><h1>" + temperature + " F</h1></li><li>Min: " + tempMin + " F Max: " + tempMax + " F</li><li>Humidity: " + humidity + "%</li>";
everything += "<li>" + description + "</li>";
everything += "</ul>";;
$("#weather").html(everything);
},
failure: function(parsed_json){
$("#displayCity").val("No City");
$("#weather").html("No Weather");
},
});
});
$("#searchButton").click(function(e){
var value = $("#searchField").val();
console.log(value);
e.preventDefault(); //will prevent the browswer from submitting the form
var myUrl = "https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=" + encodeURI(value) + "&site=stackoverflow";
console.log(myUrl);
$.ajax({
url: myUrl,
dataType: "json",
success: function(parsed_json) {
var everything = "<ul>";
var itemsArray = parsed_json["items"]; //get into the array it returns of all of the items
for(var i = 0; i < itemsArray.length; i++){
var title = itemsArray[i]["title"];
var link = itemsArray[i]["link"];
everything += "<li><a href =\"" + link + "\">" + title + "</a></li>";
}
everything += "</ul>";
$("#searchResults").html(everything);
}
});
});
});
function makeCaps(str){
str = str.charAt(0).toUpperCase() + str.slice(1);
for(var i = 0; i < str.length - 2; i++){
if(str.charAt(i) == " "){
str = str.substr(0, i + 1) + str.charAt(i + 1).toUpperCase() + str.substr(i + 2);
}
}
return str;
}