-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathpart3-underscore-refactor.js
More file actions
110 lines (95 loc) · 4.19 KB
/
Copy pathpart3-underscore-refactor.js
File metadata and controls
110 lines (95 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(function(){
var map = L.map('map', {
center: [39.9522, -75.1639],
zoom: 14
});
var Stamen_TonerLite = L.tileLayer('http://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
subdomains: 'abcd',
minZoom: 0,
maxZoom: 20,
ext: 'png'
}).addTo(map);
/* =====================
# Lab 2, Part 3
## Introduction
You've already seen this file organized and refactored. In this lab, you will
try to refactor this code to be cleaner and clearer - you should use the
utilities and functions provided by underscore.js. Eliminate loops where possible.
===================== */
// Mock user input
// Filter out according to these zip codes:
var acceptedZipcodes = [19106, 19107, 19124, 19111, 19118];
// Filter according to enrollment that is greater than this variable:
var minEnrollment = 300;
// clean data
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') {
split = schools[i].ZIPCODE.split(' ');
normalized_zip = parseInt(split[0]);
schools[i].ZIPCODE = normalized_zip;
}
// Check out the use of typeof here — this was not a contrived example.
// Someone actually messed up the data entry
if (typeof schools[i].GRADE_ORG === 'number') { // if number
schools[i].HAS_KINDERGARTEN = schools[i].GRADE_LEVEL < 1;
schools[i].HAS_ELEMENTARY = 1 < schools[i].GRADE_LEVEL < 6;
schools[i].HAS_MIDDLE_SCHOOL = 5 < schools[i].GRADE_LEVEL < 9;
schools[i].HAS_HIGH_SCHOOL = 8 < schools[i].GRADE_LEVEL < 13;
} else { // otherwise (in case of string)
schools[i].HAS_KINDERGARTEN = schools[i].GRADE_LEVEL.toUpperCase().indexOf('K') >= 0;
schools[i].HAS_ELEMENTARY = schools[i].GRADE_LEVEL.toUpperCase().indexOf('ELEM') >= 0;
schools[i].HAS_MIDDLE_SCHOOL = schools[i].GRADE_LEVEL.toUpperCase().indexOf('MID') >= 0;
schools[i].HAS_HIGH_SCHOOL = schools[i].GRADE_LEVEL.toUpperCase().indexOf('HIGH') >= 0;
}
}
// filter data
var filtered_data = [];
var filtered_out = [];
for (var i = 0; i < schools.length - 1; i++) {
isOpen = schools[i].ACTIVE.toUpperCase() == 'OPEN';
isPublic = (schools[i].TYPE.toUpperCase() !== 'CHARTER' ||
schools[i].TYPE.toUpperCase() !== 'PRIVATE');
isSchool = (schools[i].HAS_KINDERGARTEN ||
schools[i].HAS_ELEMENTARY ||
schools[i].HAS_MIDDLE_SCHOOL ||
schools[i].HAS_HIGH_SCHOOL);
meetsMinimumEnrollment = schools[i].ENROLLMENT > minEnrollment;
meetsZipCondition = acceptedZipcodes.indexOf(schools[i].ZIPCODE) >= 0;
filter_condition = (isOpen &&
isSchool &&
meetsMinimumEnrollment &&
!meetsZipCondition);
if (filter_condition) {
filtered_data.push(schools[i]);
} else {
filtered_out.push(schools[i]);
}
}
console.log('Included:', filtered_data.length);
console.log('Excluded:', filtered_out.length);
// main loop
var color;
for (var i = 0; i < filtered_data.length - 1; i++) {
isOpen = filtered_data[i].ACTIVE.toUpperCase() == 'OPEN';
isPublic = (filtered_data[i].TYPE.toUpperCase() !== 'CHARTER' ||
filtered_data[i].TYPE.toUpperCase() !== 'PRIVATE');
meetsMinimumEnrollment = filtered_data[i].ENROLLMENT > minEnrollment;
// Constructing the styling options for our map
if (filtered_data[i].HAS_HIGH_SCHOOL){
color = '#0000FF';
} else if (filtered_data[i].HAS_MIDDLE_SCHOOL) {
color = '#00FF00';
} else {
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);
}
})();