diff --git a/lab/lab1/part1.js b/lab/lab1/part1.js index 20d7636..52bf343 100644 --- a/lab/lab1/part1.js +++ b/lab/lab1/part1.js @@ -66,8 +66,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); /* ===================== @@ -75,7 +74,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); @@ -84,7 +83,7 @@ Is the first element in bakedGoods an object? Answer this question with underscore. Should evaluate to true. ===================== */ -var query3; +var query3 = _.isObject(bakedGoods[0]); console.log('The first element in bakedGoods is an object:', query3); @@ -92,15 +91,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(g){return g.price > 4.0;}); console.log('More than $4:', query5); @@ -108,7 +106,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, 'inventory'); console.log('Sorted by inventory (lowest to highest):', query6); @@ -116,7 +114,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/part1-ajax-calls.js b/lab/lab2/js/part1-ajax-calls.js index b039804..0c1c2d8 100644 --- a/lab/lab2/js/part1-ajax-calls.js +++ b/lab/lab2/js/part1-ajax-calls.js @@ -9,3 +9,13 @@ var Stamen_TonerLite = L.tileLayer('http://stamen-tiles-{s}.a.ssl.fastly.net/ton maxZoom: 20, ext: 'png' }).addTo(map); + +var promise = $.ajax('https://raw.githubusercontent.com/MUSA611-CPLN692-spring2020/datasets/master/json/philadelphia-bike-crashes-snippet.json'); +promise.done(console.log); // this prints it in json form + +var unparsed; // converting to csv +promise.done(x=> unparsed = x); // saving the json as a new variable 'unparsed' +parsed = JSON.parse(unparsed); // parsing 'unparsed' +console.log(parsed); + +// $ means jquery diff --git a/lab/lab2/js/part2-app-state.js b/lab/lab2/js/part2-app-state.js index 1ef6e76..e218f4e 100644 --- a/lab/lab2/js/part2-app-state.js +++ b/lab/lab2/js/part2-app-state.js @@ -33,19 +33,31 @@ ===================== */ // Use the data source URL from lab 1 in this 'ajax' function: -var downloadData = $.ajax("http://"); +var downloadData = $.ajax("https://raw.githubusercontent.com/MUSA611-CPLN692-spring2020/datasets/master/json/philadelphia-bike-crashes-snippet.json"); // Write a function to prepare your data (clean it up, organize it // as you like, create fields, etc) -var parseData = function() {}; +var parseData = function(data){return parsed=JSON.parse(data)}; // Write a function to use your parsed data to create a bunch of // marker objects (don't plot them!) -var makeMarkers = function() {}; +var createMarkers = function(crash){ + var lat = crash.lat_final; + var long = crash.long_final; + var marker = L.marker([lat,long]); + return marker; +}; + +var makeMarkers = function(parsedData) { + markers = _.map(parsedData, createMarkers); + return markers; +}; // Now we need a function that takes this collection of markers // and puts them on the map -var plotMarkers = function() {}; +var plotMarkers = function(markers) { + _.map(markers, function(m){m.addTo(map)}); +}; // At this point you should see a bunch of markers on your map if // things went well. @@ -66,9 +78,11 @@ var plotMarkers = function() {}; // Look to the bottom of this file and try to reason about what this // function should look like -var removeMarkers = function() {}; +var removeMarkers = function(markers) { + _.map(markers, function(x){map.removeLayer(x)}); +}; -/* ===================== +/* ==============2wsx======= Optional, stretch goal Write the necessary code (however you can) to plot a filtered down version of the downloaded and parsed data.