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

HW#3_Mengting_Yu #5

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: 12 additions & 3 deletions lab/lab1/part1-array-access.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,19 @@
Start code

===================== */
var restaurantData = [
[39.953761, -75.193587, 'White Dog Cafe'],
[39.890218, -75.177059, 'Honey Grow'],
[39.958091, -75.225286, 'Just Salad']
]

restaurantData.push([39.958125, -75.235425, 'KFC'], [39.956784, -75.189457, 'Spring Chinese']);

for (i = 0; i<restaurantData.length; i++) {
L.marker([restaurantData[i][0],restaurantData[i][1]]).addTo(map).bindPopup("<b>" + restaurantData[i][2] + "</b>");
}


L.marker([0, 0]).addTo(map);
L.marker([0, 0]).addTo(map);
L.marker([0, 0]).addTo(map);

/* =====================

Expand Down
17 changes: 16 additions & 1 deletion lab/lab1/part2-fizzbuzz.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@
Start code

===================== */

var num = [];
for (i = 1; i<=100; i++) {
if (i % 3 === 0) {
num.push('Fizz')
}
else if (i % 5 === 0) {
num.push('Buzz')
}
else if (i % 15 === 0) {
num.push('FizzBuzz')
}
else {
num.push(i)
}
}
console.log(num)


/* =====================
Expand Down
44 changes: 39 additions & 5 deletions lab/lab1/part3-data-transformation.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

<!--javascript imports-->
<script src="leaflet.js"></script>
<script src="part4-data-clean.js"></script>
<script src="part4-data-dirty.js"></script>
<script src="part3-data-clean.js"></script>
<script src="part3-data-dirty.js"></script>

<script>

Expand Down Expand Up @@ -62,15 +62,45 @@
if their formatting is especially wrong. But it should accommodate most points.

===================== */

var data = bikeArrayClean;
//var data = bikeArrayClean;
var data = bikeArrayDirty;

/* =====================

Start code to filter data

===================== */

//Clean data
/*var dataFiltered = [];
for (i = 0; i<data.length; i++) {
if (data[i][3]>=20) {
dataFiltered.push(data[i])
}
}
console.log(dataFiltered)*/

//Dirty data
var dataFiltered = [];
for (i = 0; i<data.length; i++) {
if (data[i].length==5) {
delete data[i][0]
data[i][0] = Number(data[i][0])
data[i][1] = Number(data[i][1])
data[i][3] = parseInt(data[i][3], 10)
if (data[i][3]>20) {
dataFiltered.push(data[i])
}
}
else if (data[i].length==4) {
data[i][0] = Number(data[i][0])
data[i][1] = Number(data[i][1])
data[i][3] = parseInt(data[i][3], 10)
if (data[i][3]>=20) {
dataFiltered.push(data[i])
}
}
}
console.log(dataFiltered)


/* =====================
Expand Down Expand Up @@ -98,6 +128,10 @@

===================== */

for (i = 0; i<dataFiltered.length; i++) {
L.marker([dataFiltered[i][1],dataFiltered[i][0]]).addTo(map).bindPopup("<b>" + dataFiltered[i][2] + ', ' + dataFiltered[i][3] + ' docks' + "</b>");
}



/* =====================
Expand Down
32 changes: 32 additions & 0 deletions lab/lab1/part4-project-euler.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@
Start code

===================== */
//Problem 1
let sum = 0;
for (i = 0; i < 1000; i++) {
if (i % 3 === 0) {
sum += i
}
else if (i % 5 === 0) {
sum += i
}
}
console.log(sum)

//Problem 2
function fibonacci(num) {
if (num <= 1) return 1;
else {
return fibonacci(num - 1) + fibonacci(num - 2);
Copy link
Member

Choose a reason for hiding this comment

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

This is nice. Took me a second to wrap my head around this.

}
}

let a = 1;
let n = 0;
while (fibonacci(a)<4000000) {
if (fibonacci(a) % 2===0) {
n += fibonacci(a)
}
a++;
}
console.log(n)
console.log(a)
console.log(fibonacci(a))




Expand Down
17 changes: 9 additions & 8 deletions lab/lab2/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,63 @@ console.log('Nathan\'s list', nathanGameList);
What is the first game in Ross's list?
===================== */

var query1;

var query1 = _.first(rossGameList);

console.log('What is the first game in Ross\'s list?', query1);

/* =====================
What are all of the games except for the first game in ross's list?
===================== */

var query2;
var query2 = _.rest(rossGameList);

console.log('What are all of the games except for the first game in Ross\'s list?', query2);

/* =====================
What is the last game in Nathan's list?
===================== */

var query3;
var query3 = _.last(nathanGameList);

console.log('What is the last game in Nathan\'s list?', query3);

/* =====================
What are all of the games in Nathan's list except for the last?
===================== */

var query4;
var query4 = _.initial(nathanGameList);

console.log('What are all of the games in Nathan\'s list except for the last?', query4);

/* =====================
What would Nathan's game list look like if he sold "catan"?
===================== */

var query5;
var query5 = _.without(nathanGameList, "catan");

console.log('What would Nathan\'s game list look like if he sold "catan"?', query5);

/* =====================
If Nathan and Ross play a board game, what are their options? This should be a list of all games owned by ross or Nathan, with no duplicates.
===================== */

var query6;
var query6 = _.union(rossGameList, nathanGameList);

console.log('If Nathan and Ross play a board game, what are their options? This should be a list of all games owned by ross or Nathan, with no duplicates.', query6);

/* =====================
Which games are owned by both Ross and Nathan?
===================== */

var query7;
var query7 = _.intersection(rossGameList, nathanGameList);

console.log('Which games are owned by both Ross and Nathan', query7);

/* =====================
Which games are exclusive to collections? In other words, only owned by either Ross or Nathan.
===================== */

var query8;
var query8 = _.union(_.difference(rossGameList, nathanGameList), _.difference(nathanGameList, rossGameList));

console.log('Which games are exclusive to one collection? In other words, only owned by either Ross or Nathan (but not both!).', query8);
75 changes: 71 additions & 4 deletions lab/lab2/part4-underscore-refactor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function(){
(function () {

var map = L.map('map', {
center: [39.9522, -75.1639],
Expand Down Expand Up @@ -30,9 +30,76 @@
// Filter according to enrollment that is greater than this variable:
var minEnrollment = 300;

// refactored codes of clean data
schools = _.each(schools, function(s) {
if (_.isString(s.ZIPCODE)) {
split = s.ZIPCODE.split(' ');
normalized_zip = parseInt(split[0]);
s.ZIPCODE = normalized_zip;
}
});

schools = _.each(schools, function(g) {
if (_.isNumber(g.GRADE_ORG)) {
g.HAS_KINDERGARTEN = g.GRADE_LEVEL < 1;
g.HAS_ELEMENTARY = 1 < g.GRADE_LEVEL < 6;
g.HAS_MIDDLE_SCHOOL = 5 < g.GRADE_LEVEL < 9;
g.HAS_HIGH_SCHOOL = 8 < g.GRADE_LEVEL < 13;
}
else if (!_.isNumber(g.GRADE_ORG)) {
g.HAS_KINDERGARTEN = g.GRADE_LEVEL.toUpperCase().indexOf('K') >= 0;
g.HAS_ELEMENTARY = g.GRADE_LEVEL.toUpperCase().indexOf('ELEM') >= 0;
g.HAS_MIDDLE_SCHOOL = g.GRADE_LEVEL.toUpperCase().indexOf('MID') >= 0;
g.HAS_HIGH_SCHOOL = g.GRADE_LEVEL.toUpperCase().indexOf('HIGH') >= 0;
}
});

// refactored codes of filter data
var filtered_data = _.filter(schools, function(f) {
isOpen = f.ACTIVE.toUpperCase() == 'OPEN';
isPublic = (f.TYPE.toUpperCase() !== 'CHARTER' ||
f.TYPE.toUpperCase() !== 'PRIVATE');
isSchool = (f.HAS_KINDERGARTEN ||
f.HAS_ELEMENTARY ||
f.HAS_MIDDLE_SCHOOL ||
f.HAS_HIGH_SCHOOL);
meetsMinimumEnrollment = f.ENROLLMENT > minEnrollment;
meetsZipCondition = acceptedZipcodes.indexOf(f.ZIPCODE) >= 0;
filter_condition = (isOpen &&
isSchool &&
meetsMinimumEnrollment &&
!meetsZipCondition);
return filter_condition;
});
var filtered_out = _.difference(schools, filtered_data);

console.log('Included:', filtered_data.length);
console.log('Excluded:', filtered_out.length);

// refactored codes of main loop
_.each(filtered_data, function(c) {
isOpen = c.ACTIVE.toUpperCase() == 'OPEN';
isPublic = (c.TYPE.toUpperCase() !== 'CHARTER' ||
c.TYPE.toUpperCase() !== 'PRIVATE');
meetsMinimumEnrollment = c.ENROLLMENT > minEnrollment;
if (c.HAS_HIGH_SCHOOL) {
color = '#0000FF';
}
else if (c.HAS_MIDDLE_SCHOOL) {
color = '#00FF00';
}
else {
color = '##FF0000';
}
pathOpts = {'radius': c.ENROLLMENT / 30,
'fillColor': color};
L.circleMarker([c.Y, c.X], pathOpts)
.bindPopup(c.FACILNAME_LABEL)
.addTo(map);
});

// clean data
for (var i = 0; i < schools.length - 1; i++) {
/*for (var i = 0; i < schools.length - 1; i++) {
// If we have '19104 - 1234', splitting and taking the first (0th) element
// as an integer should yield a zip in the format above
if (typeof schools[i].ZIPCODE === 'string') {
Expand Down Expand Up @@ -97,14 +164,14 @@
} else if (filtered_data[i].HAS_MIDDLE_SCHOOL) {
color = '#00FF00';
} else {
color = '##FF0000';
color = '##FF0000'
}
// The style options
var pathOpts = {'radius': filtered_data[i].ENROLLMENT / 30,
'fillColor': color};
L.circleMarker([filtered_data[i].Y, filtered_data[i].X], pathOpts)
.bindPopup(filtered_data[i].FACILNAME_LABEL)
.addTo(map);
}
}*/

})();