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

Zixuan W_WEEK4 #24

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
15 changes: 7 additions & 8 deletions lab/lab1/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ 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 +72,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 +81,38 @@ Is the first element in bakedGoods an object? Answer this question with
underscore. Should evaluate to true.
===================== */

var query3;
var query3 = _.isObject(_.first(bakedGoods));

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(num){ return num.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(list){ return list['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 Down
Empty file added lab/lab2/js/.Rhistory
Empty file.
60 changes: 56 additions & 4 deletions lab/lab2/js/part1-ajax-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
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 +31,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 @@ -95,6 +96,57 @@ var phillyCrimeDataUrl = "https://raw.githubusercontent.com/CPLN692-MUSA611/data
var phillyBikeCrashesDataUrl = "https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-bike-crashes-snippet.json";



// var trythefirst = function() {
// $.ajax("https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-crime-snippet.json")
// .done(function(ajaxResponseValue) {
// // a function that does some kind of transformation on the response
// console.log(ajaxResponseValue)
// CrimeData = ajaxResponseValue;
// _.forEach(CrimeData, function(x){
// // popupText = x.["General Crime Category"],
// // L.marker([x.["Lat"], x.["Lng"]]).addTo(map);});
// });
// };)
// };

var crimeData;
var trythefirst = function() {
$.ajax("https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-crime-snippet.json")
.done(function(ajaxResponseValue) {
crimeData = JSON.parse(ajaxResponseValue);
// return crimeData
console.log(crimeData);
_.forEach(crimeData, function(x) {
L.marker([x.Lat, x.Lng]).addTo(map)});
})
}
trythefirst();

var solarData;
var trytheSecond = function() {
$.ajax("https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-solar-installations.json")
.done(function(ajaxResponseValue) {
solarData = JSON.parse(ajaxResponseValue);
// return crimeData
console.log(solarData);
_.forEach(solarData, function(x) {
L.marker([x.X, x.Y]).addTo(map)});})
}
trytheSecond();

var bikeData;
var trythelast = function() {
$.ajax("https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-bike-crashes-snippet.json")
.done(function(ajaxResponseValue) {
solarData = JSON.parse(ajaxResponseValue);
// return crimeData
console.log(solarData);
_.forEach(solarData, function(x) {
L.marker([x.lat_final, x.long_final]).addTo(map)});
})
}
trythelast();
/* =====================
Data you grab through ajax is just text. You'll need to parse it as javascript
objects to really work with it. Use the function `JSON.parse` on the string you
Expand Down
22 changes: 17 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,19 @@
===================== */

// 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(parsed) {
var markers = [];
_.forEach(parsed, function(j){
markers.push(L.marker([j.Lat, j.Lng]))
});
return markers;
};
var plotMarkers = function(markers) {
_.forEach(markers, function(k) {
k.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.

image

Looks like there is an error with a { somewhere.

But otherwise, looks good.



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

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

/* =====================
Optional, stretch goal
Expand Down
31 changes: 24 additions & 7 deletions lab/lab2/js/part3-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,45 @@
/* =====================
Define a resetMap function to remove markers from the map and clear the array of markers
===================== */
var resetMap = function() {
/* =====================
Fill out this function definition
===================== */
var resetMap = function(myMarkers) {
_.forEach(myMarkers, function(k) {
map.removeLayer(k);
});
};

/* =====================
Define a getAndParseData function to grab our dataset through a jQuery.ajax call ($.ajax). It
will be called as soon as the application starts. Be sure to parse your data once you've pulled
it down!
===================== */
var crimelist;
var getAndParseData = function() {
/* =====================
Fill out this function definition
===================== */
$.ajax("https://raw.githubusercontent.com/CPLN692-MUSA611/datasets/master/json/philadelphia-crime-snippet.json")
.done(function(res) {
crimelist = JSON.parse(res);
});
};

/* =====================
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)
===================== */
var filterd = [];
var myfilter = function(list) {
for (i = 0; i < list.length; i++) {
//filter a certain type of crime within certain districts, and within certain Police Service Areas (PSA 1: checked, PSA 2: unchecked)
if (list[i]['UCR Code']>= numericField1
&& list[i]['UCR Code'] <= numericField2) {
filterd.push(list[i]);
}
}
return filterd;
};

var plotData = function() {
myMarkers = _.each(myfilter(crimelist), (x) => {
L.marker([x.Lat, x.Lng]).addTo(map).bindPopup(x['General Crime Category']);
});
/* =====================
Fill out this function definition
===================== */
Expand Down