Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW4-Zixuan Xu #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion example/js/indego-bike.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var cleanStations;
var getBikeData = function() {
$.ajax(rideIndego).done(function(ajaxResponseValue) {
// a function that does some kind of transformation on the response
bikeData = ajaxResponseValue;
bikeData = JSON.parse(ajaxResponseValue);
// Turn the ajax json return into a familiar object format
stations = new Object(bikeData.features);
// removing the `geometries` to make it easier to show off some underscore examples
Expand Down
21 changes: 13 additions & 8 deletions lab/lab1/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ var printMenu = function(foodList) {

console.log('List of baked goods', bakedGoods);


/* =====================
Is printMenu a function? Answer this question with underscore. Should evaluate
to true.
===================== */

var query1;
var query1 = _.isFunction(printMenu);

console.log('printMenu is a function:', query1);

Expand All @@ -72,7 +73,7 @@ Is bakedGoods an array? Answer this question with underscore. Should evaluate
to true.
===================== */

var query2;
var query2 = _.isArray(bakedGoods);

console.log('bakedGoods is an array:', query2);

Expand All @@ -81,39 +82,43 @@ Is the first element in bakedGoods an object? Answer this question with
underscore. Should evaluate to true.
===================== */

var query3;
var query3 = _.isObject(bakedGoods,[1]);

console.log('The first element in bakedGoods is an object:', query3);

/* =====================
Use _.where to return all cakes. Or bread. Whichever is your favorite.
===================== */

var query4;
var query4 = _.where(bakedGoods, {"type":"Cake"});

console.log('All bread. Or cakes:', query4);

/* =====================
Use _.filter to return all baked goods that cost more than $4.
===================== */

var query5;
var query5 = _.filter(bakedGoods, function(good){
return good['price'] > 4;
});

console.log('More than $4:', query5);

/* =====================
Use _.sortBy to order the list by inventory (from lowest to highest).
===================== */

var query6;
var query6 = _.sortBy(bakedGoods, function(good){
return good['inventory']
});

console.log('Sorted by inventory (lowest to highest):', query6);

/* =====================
Use _.groupBy to organize the baked goods by type.
===================== */

var query7;
var query7 = _.groupBy(bakedGoods, 'type');

console.log('Grouped by type:', query7);

Expand All @@ -137,7 +142,7 @@ Rye ... $5.09
Whole Wheat ... $4.49
===================== */

// printMenu(query7);
printMenu(query7);

/* =====================
Stretch Goal:
Expand Down
35 changes: 30 additions & 5 deletions lab/lab2/js/part1-ajax-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
This recipe, can be used by underscore's _.filter. It will return only words with
>=5 characters.
===================== */
var isLengthOfFiveOrMore = function(str) {};
var isLengthOfFiveOrMore = function(str) {
return str.length >= 5;
};

console.log("isLengthOfFiveOrMore success:",
_.isEqual(_.filter(['this', 'is','a', 'test', 'testing'], isLengthOfFiveOrMore), ['testing']));
Expand All @@ -30,15 +32,20 @@ console.log("isLengthOfFiveOrMore success:",
function you write along with underscore's _.each to log the double of every
number in the provided array.
===================== */
var logDouble = function(num) {};
var logDouble = function(num) {
console.log(num * 2);
};
var theArray = [1, 5, 20, 100];
_.each(theArray, function(num){(logDouble(num))});




/* =====================
Given this already defined function, define fizzbuzzArray so that, when mapped
over, it will equal ['fizz', 'buzz', 'fizzbuzz'];
===================== */
var fizzbuzzArray = [];
var fizzbuzzArray = [3,5,15];
var fizzbuzzFunc = function(num) {
var str = '';
if (num % 3 === 0) { str = 'fizz'; }
Expand Down Expand Up @@ -105,16 +112,34 @@ var phillyBikeCrashesDataUrl = "https://raw.githubusercontent.com/CPLN692-MUSA61
===================== */



var popupText;
var crimeData;
var crimes;






/* =====================
Now that you've properly parsed your data, use _.each to plot the
dataset you've pulled down.
===================== */


var getCrimeData = function() {
$.ajax(phillyCrimeDataUrl).done(function(ajaxResponseValue) {
crimeData = JSON.parse(ajaxResponseValue);
console.log(crimeData);
_.forEach(crimeData, function(x){
L.marker([x.Lat, x.Lng]).addTo(map)});
});
};
getCrimeData();
/* =====================
Leaflet setup - feel free to ignore this
===================== */


var map = L.map('map', {
center: [39.9522, -75.1639],
zoom: 14
Expand Down
27 changes: 22 additions & 5 deletions lab/lab2/js/part2-app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,23 @@
===================== */

// We set this to HTTP to prevent 'CORS' issues
var downloadData = $.ajax("http://");
var parseData = function() {};
var makeMarkers = function() {};
var plotMarkers = function() {};
var downloadData = $.ajax("https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-crime-snippet.json");
var parseData = function(data) {
return JSON.parse(data)
};
var makeMarkers = function(parseData) {
var markers = [];
_.forEach(parseData, function(x){
Copy link
Member

Choose a reason for hiding this comment

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

Small improvement suggestion: using _.map instead of _.each, which creates the array and doesn't require the push

markers.push(L.marker([x.Lat, x.Lng]))
});
return markers;
};
var plotMarkers = function(markers) {
_.forEach(markers, function(m) {
m.addTo(map);
});
};



/* =====================
Expand All @@ -53,7 +66,11 @@ var plotMarkers = function() {};
user's input.
===================== */

var removeMarkers = function() {};
var removeMarkers = function(markers) {
_.forEach(markers, function(m) {
map.removeLayer(m)
});
};

/* =====================
Optional, stretch goal
Expand Down
22 changes: 20 additions & 2 deletions lab/lab2/js/part3-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
Remember, this is open-ended. Open ended doesn't mean pointless: we're looking for
filters that might actually be useful. Try to see what you can produce.
===================== */

var phillyCrimeDataUrl = "https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-crime-snippet.json";
/* =====================
Define a resetMap function to remove markers from the map and clear the array of markers
===================== */
var resetMap = function() {
var resetMap = function(myMarkers) {
Copy link
Member

Choose a reason for hiding this comment

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

I can't get your resetMap() to work.

I think it's because myMarkers has undefined elements in, as per this screen shot:
image

I'm not exactly sure why you have undefineds in there because your conditional logic looks good to filter those out. But alas, it has undefined.

_.forEach(myMarkers, function(m) {
map.removeLayer(m);
});
/* =====================
Fill out this function definition
===================== */
Expand All @@ -39,6 +42,9 @@ var resetMap = function() {
it down!
===================== */
var getAndParseData = function() {
$.ajax(phillyCrimeDataUrl).done(function(res) {
myData = JSON.parse(res);
});
/* =====================
Fill out this function definition
===================== */
Expand All @@ -49,6 +55,18 @@ var getAndParseData = function() {
criteria happens to be — that's entirely up to you)
===================== */
var plotData = function() {
_.forEach(myData, function(x){
var marker;
if (booleanField && x["UCR Code"] == 600) {
marker = L.marker([x.Lat, x.Lng]).bindPopup(x["General Crime Category"]).addTo(map);
Copy link
Member

Choose a reason for hiding this comment

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

I hadn't requested clarifying comments before this HW was due... but for next time, it'd be helpful to have some comments to help understand what it is you are trying to filter here by so I could get a good idea from the outset without needing to understand all the logic.

But it looks good.

if (numericField1 !== "" && numericField2 !== "" && x.PSA > numericField1 && x.PSA < numericField2) {
marker = L.marker([x.Lat, x.Lng]).bindPopup("PSA - " + x.PSA + ": " + x["General Crime Category"]).addTo(map);
}}
if (stringField !== "" && x["General Crime Category"] == stringField) {
marker = L.marker([x.Lat, x.Lng]).bindPopup(x["General Crime Category"]).addTo(map);
}
myMarkers.push(marker);
});
/* =====================
Fill out this function definition
===================== */
Expand Down
6 changes: 3 additions & 3 deletions lab/lab2/part3-application.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
<!--Left Panel-->
<div style="position: absolute; left: 0px; width: 400px; top: 0; bottom: 0;">
<br>
Numeric filter: <input id="num1" type="number" name="num1" value=""> to <input id="num2" type="number" name="num2" value="">
PSA: <input id="num1" type="number" name="num1" value=""> to <input id="num2" type="number" name="num2" value="">
<br>
String filter: <input id="string" type="text">
Crime Category: <input id="string" type="text">
<br>
Boolean filter: <input id="boolean" type="checkbox">
Thefts: <input id="boolean" type="checkbox">
<br>
<button id="my-button">Plot Data</button>
</div>
Expand Down