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

Lufeng Lin-week4 #19

Open
wants to merge 3 commits 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
14 changes: 7 additions & 7 deletions lab/lab1/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ to true.
===================== */

var query1;

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

/* =====================
Expand All @@ -73,7 +73,7 @@ to true.
===================== */

var query2;

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

/* =====================
Expand All @@ -82,39 +82,39 @@ underscore. Should evaluate to true.
===================== */

var query3;

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;

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;

query5 = _.filter(bakedGoods, function(i){return i.price>4});
console.log('More than $4:', query5);

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

var query6;

query6 = _.sortBy(bakedGoods, function(j){ return Math.sin(j.inventory).reverse;});
console.log('Sorted by inventory (lowest to highest):', query6);

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

var query7;

query7 = _.groupBy(bakedGoods, function(x){ return x.type; });
console.log('Grouped by type:', query7);

/* =====================
Expand Down
33 changes: 28 additions & 5 deletions lab/lab2/js/part1-ajax-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
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 +29,15 @@ 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 @@ -94,6 +93,30 @@ var phillySolarInstallationDataUrl = "https://raw.githubusercontent.com/CPLN692-
var phillyCrimeDataUrl = "https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-crime-snippet.json";
var phillyBikeCrashesDataUrl = "https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-bike-crashes-snippet.json";

var crimedata;
var layerGroup

// async ajax call
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);
});
})
}


// run the ajax function
getCrimeData();
// create a feature group to provide additional controls over the markers
// read more about layer groups here: https://leafletjs.com/examples/layers-control/
// It makes it easier to control a group of markers


/* ---- THIS IS HOW YOU CLEAR A LAYER GROUP ----- */
// layerGroup.clearLayers();

/* =====================
Data you grab through ajax is just text. You'll need to parse it as javascript
Expand Down
25 changes: 19 additions & 6 deletions lab/lab2/js/part2-app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,28 @@
===================== */

// 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(downloadedData) {
return JSON.parse(downloadedData);
};
var makeMarkers = function(parsedData) {
var maker = [];
_.forEach(parsedData, function(x){
maker.push(L.marker([x.Lat, x.Lng]));
});
return maker;
};
var plotMarkers = function(y) {
_.forEach(y, function(z){
z.addTo(map);
});
};


/* =====================
Define the function removeData so that it clears the markers you've written
from the map. You'll know you've succeeded when the markers that were
previously displayed are immediately removed from the map.
previously displayed are immediately removed fr om the map.

In Leaflet, the syntax for removing one specific marker looks like this:

Expand Down Expand Up @@ -84,8 +96,9 @@ var Stamen_TonerLite = L.tileLayer('http://stamen-tiles-{s}.a.ssl.fastly.net/ton
===================== */

downloadData.done(function(data) {
console.log(data);
var parsed = parseData(data);
var markers = makeMarkers(parsed);
plotMarkers(markers);
removeMarkers(markers);
// removeMarkers(markers);
Copy link
Member

Choose a reason for hiding this comment

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

You did not create the removeMarkers function

});
28 changes: 28 additions & 0 deletions lab/lab2/js/part3-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
Define a resetMap function to remove markers from the map and clear the array of markers
===================== */
var resetMap = function() {
_.forEach(myMarkers, function(x) {
map.removeLayer(x);
});
myMarkers = null;
/* =====================
Fill out this function definition
===================== */
Expand All @@ -42,14 +46,38 @@ var getAndParseData = function() {
/* =====================
Fill out this function definition
===================== */
var Crimedata = "http://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-crime-snippet.json";
var y = $.ajax(Crimedata).done(function(y) {
myData = JSON.parse(y);
});
};

/* =====================
Call our plotData function. It should plot all the markers that meet our criteria (whatever that
criteria happens to be — that's entirely up to you)
===================== */

/*=====================
Fill out this function definition
=====================*/
console.log(newMarkers);*/
Copy link
Member

Choose a reason for hiding this comment

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

newMarkers is not defined.

var markerfilter = function(y) {
var markerfilterd = [];
for (i = 0; i < y.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

Are you able to add points to the map with this filter?

What parameters do you set?

if (y[i]['UCR Code'] >= numericField1 && y[i]['UCR Code'] <= numericField2 && y[i]['General Crime Category'] == stringField &&
(y[i].District == 24) == booleanField) {
markerfilterd.push(y[i]);
}
}
console.log("Number of crimes: " + markerfilterd.length);
return markerfilterd;
};

var plotData = function() {
/* =====================
Fill out this function definition
===================== */
myMarkers = _.each(markerfilter(myData), (z) => {
L.marker([z.Lat, z.Lng]).addTo(map).bindPopup(z['Location Block']);
});
};
1 change: 1 addition & 0 deletions lab/lab2/js/part3-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $('button#my-button').click(function(e) {

stringField = $('#string').val();
console.log("stringField", stringField);
console.log(myData)


/* =====================
Expand Down