diff --git a/lab/lab1/part1.js b/lab/lab1/part1.js index da84f3d..261b1e8 100644 --- a/lab/lab1/part1.js +++ b/lab/lab1/part1.js @@ -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); @@ -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); @@ -81,7 +81,7 @@ 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); @@ -89,15 +89,14 @@ 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); @@ -105,7 +104,7 @@ 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); @@ -113,7 +112,7 @@ 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); diff --git a/lab/lab2/js/.Rhistory b/lab/lab2/js/.Rhistory new file mode 100644 index 0000000..e69de29 diff --git a/lab/lab2/js/part1-ajax-calls.js b/lab/lab2/js/part1-ajax-calls.js index 575a13c..291a49d 100644 --- a/lab/lab2/js/part1-ajax-calls.js +++ b/lab/lab2/js/part1-ajax-calls.js @@ -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'])); @@ -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'; } @@ -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 diff --git a/lab/lab2/js/part2-app-state.js b/lab/lab2/js/part2-app-state.js index e342fe3..2c277a8 100644 --- a/lab/lab2/js/part2-app-state.js +++ b/lab/lab2/js/part2-app-state.js @@ -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); + }); /* ===================== @@ -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 diff --git a/lab/lab2/js/part3-application.js b/lab/lab2/js/part3-application.js index 8572d7d..964b4ec 100644 --- a/lab/lab2/js/part3-application.js +++ b/lab/lab2/js/part3-application.js @@ -27,10 +27,10 @@ /* ===================== 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); + }); }; /* ===================== @@ -38,17 +38,34 @@ var resetMap = function() { 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 ===================== */