Skip to content
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
24 changes: 15 additions & 9 deletions lab/lab1/part1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* =====================
# Lab 1, Part 1 — Function Review
===================== */

/* =====================
Instructions: "Write a function that adds one to the number provided"
Example: "plusOne(2) should return 3"
===================== */

var plusOne = function() {};
var plusOne = function (n) {
return (n + 1);
};

console.log('plusOne success:', plusOne(99) === 100);

Expand All @@ -17,7 +17,7 @@ Instructions: "Write a function, age, that takes a birth year and returns an age
Example: "age(2000) should return 18"
===================== */

var age = function(birth) {};
var age = function(birth) {return 2017-birth;};

console.log('age success:', age(1971) === 46);

Expand All @@ -26,7 +26,7 @@ Instructions: "Write a function that returns true for numbers over 9000 and fals
Example: "over9000(22) should return false"
===================== */

var over9000 = function() {};
var over9000 = function(num) {if (num>9000) return true; else return false;};

console.log('over9000 success:', over9000(9001) === true && over9000(12) === false);

Expand All @@ -35,7 +35,7 @@ Instructions: "Write a function that returns the value of an object at a specifi
Example: "valueAtKey({'name': 'Nathan'}, 'name') should return 'Nathan'"
===================== */

var valueAtKey = function() {};
var valueAtKey = function(obj,key) {return obj[key];};

console.log('valueAtKey success:', valueAtKey({'foo': 'bar'}, 'foo') === 'bar');

Expand All @@ -45,7 +45,7 @@ Example: "y(0, 0, 0) should return 0; y(1, 1, 1) should return 2"
Remember: The standard mathematical expression for such a function is y=mx+b
===================== */

var y = function() {};
var y = function(m,x,b) {return m*x+b;};

console.log('y success:', y(12, 1, 12) === 24);

Expand All @@ -54,7 +54,13 @@ Instructions: "Write a function which counts the number of times a value occurs
Example: "countItem(['a', 'b', 'a'], 'a') should return 2"
===================== */

var countItem = function() {};
var countItem = function(arr, char) {
var n=0;
for( i=0; i<arr.length; i++){
if (arr[i]==char) n++;
else continue;
}
return n;
};

console.log('countItem success:', countItem([1, 2, 3, 4, 5, 4, 4], 4) === 3);

37 changes: 25 additions & 12 deletions lab/lab1/part2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Functions that `return` can be passed as values to other functions. Each exercis
Instructions: Write a function that *always* returns the number 1.
===================== */

var justOne = function() {};
var justOne = function() {return 1};

console.log('justOne success:', justOne() === 1);

/* =====================
Instructions: Write a function that returns true if a number is even.
===================== */

var isEven = function() {};
var isEven = function(a) {if (a%2==0) return true; else return false;};

console.log('isEven success:', isEven(2) === true && isEven(3) === false);

Expand All @@ -24,15 +24,15 @@ Instructions: Write a function that *always* returns false.
Use functions "justOne" and "isEven" somehow in the definition.
===================== */

var justFalse = function() {};
var justFalse = function() {return false;};

console.log('justFalse success:', justFalse() === false);

/* =====================
Instructions: Write a function that takes a boolean value and returns its opposite.
===================== */

var not = function() {};
var not = function(a) {if (a) return false; else return true;};

console.log('not success:', not(true) === false);

Expand All @@ -41,20 +41,26 @@ Instructions: Write a function that returns true if a number is odd
Use functions "isEven" and "not" somehow in the definition.
===================== */

var isOdd = function() {};
var isOdd = function(a) {if (not(isEven(a))) return true; else return false;};

console.log('isOdd success:', isOdd(4) === false);

/* =====================
Instructions: Write a function that takes a list of numbers and returns a list with only numbers above 10
===================== */

var filterOutLessThan10 = function() {};
var filterOutLessThan10 = function(arr) {
var holder=[];
for(i=0;i<arr.length;i++){
if (arr[i]>10) holder.push(arr[i]);
}
return holder;
};
// The function 'arraysEqual' (which it is your task to define) is necessary because
// ([4] === [4]) is *false* in javascript(!!!)
// Use google + stackoverflow to figure out how to define a function which returns true given two equal arrays
function arraysEqual(arr1, arr2) { return false; }
console.log('filterOutLessThan10 success:', arraysEqual([4, 11], [11]));
function arraysEqual(arr1, arr2) {return arr1.toString()===arr2.toString();}
console.log('filterOutLessThan10 success:', arraysEqual(filterOutLessThan10([4, 11]), [11]));

/* =====================
Stretch goal
Expand All @@ -63,7 +69,14 @@ Instructions: Let's bring it all together. Write a function that filters a list
2. a function that takes a value and returns true (to keep a number) or false (to toss it out)
===================== */

var filter = function(array, func) {};

console.log('filter success:', filter([4, 11], isOdd) === [11]);

var filter = function(array, func) {
var holder=[];
for( var i=0; i<array.length; i++){
if(func(array[i])==true){
holder.push(array[i]);
}
}
return holder.toString();
};

console.log('filter success:', arraysEqual(filter([4, 11], isOdd), [11]));
16 changes: 8 additions & 8 deletions lab/lab2/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,62 @@ console.log('Nathan\'s list', nathanGameList);
What is the first game in Jeff's list?
===================== */

var query1;
var query1=_.first(jeffGameList);

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

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

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

console.log('What are all of the games except for the first game in Jeff\'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 Jeff play a board game, what are their options? This should be a list of all games owned by Jeff or Nathan, with no duplicates.
===================== */

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

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

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

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

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

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

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

console.log('Which games are exclusive to one collection? In other words, only owned by either Jeff or Nathan (but not both!).', query8);
6 changes: 4 additions & 2 deletions lab/lab2/part2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Calculate the value by using _.countBy and set your answer to variable "largeSta

var data = bikeArrayClean;

var largeStationList;
var largeStationList= _.filter(data, function(num){return num[3]>20;});
console.log(largeStationList);

var largeStationCount;
largeStationCount = _.countBy(data, function(list) { return list[3] > 20 ? 'More than 20': 'Less than or equal 20';});
console.log(largeStationCount);
6 changes: 3 additions & 3 deletions lab/lab2/part3-underscore-refactor.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<!-- Stylesheet Imports -->
<!-- Fix this -->
<link rel="stylesheet" href="../../assignment/leaflet.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css" />
</head>
<body>

Expand All @@ -15,8 +15,8 @@
<!-- Javascript Imports -->
<!-- Fix these -->
<script src="../../data/phillySchools.js"></script>
<script src="../../assignment/leaflet.js"></script>
<script src="../../assignment/underscore.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
<script src="part3-underscore-refactor.js"></script>

<!-- =====================
Expand Down
87 changes: 40 additions & 47 deletions lab/lab2/part3-underscore-refactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,79 +32,72 @@


// 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;
}
schools = _.map(schools, function(row)
{
if (typeof row.ZIPCODE === 'string'){
split = row.ZIPCODE.split(' ');
normalized_zip = parseInt(split[0]);
row.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;
if (typeof row.GRADE_ORG === 'number') { // if number
row.HAS_KINDERGARTEN = row.GRADE_LEVEL < 1;
row.HAS_ELEMENTARY = 1 < row.GRADE_LEVEL < 6;
row.HAS_MIDDLE_SCHOOL = 5 < row.GRADE_LEVEL < 9;
row.HAS_HIGH_SCHOOL = 8 < row.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;
row.HAS_KINDERGARTEN = row.GRADE_LEVEL.toUpperCase().indexOf('K') >= 0;
row.HAS_ELEMENTARY = row.GRADE_LEVEL.toUpperCase().indexOf('ELEM') >= 0;
row.HAS_MIDDLE_SCHOOL = row.GRADE_LEVEL.toUpperCase().indexOf('MID') >= 0;
row.HAS_HIGH_SCHOOL = row.GRADE_LEVEL.toUpperCase().indexOf('HIGH') >= 0;
}
return row;
}

);
// 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);

_.map(schools,function(row){
isOpen = row.ACTIVE.toUpperCase() == 'OPEN';
isPublic = _.some([row.TYPE.toUpperCase() !== 'CHARTER',
row.TYPE.toUpperCase() !== 'PRIVATE']);
isSchool = _.some([row.HAS_KINDERGARTEN,row.HAS_ELEMENTARY,row.HAS_MIDDLE_SCHOOL,row.HAS_HIGH_SCHOOL]);
meetsMinimumEnrollment = row.ENROLLMENT > minEnrollment;
meetsZipCondition = _.contains(acceptedZipcodes,row.ZIPCODE);
filter_condition = _.every([isOpen,isSchool,meetsMinimumEnrollment,!meetsZipCondition]);
if (filter_condition) {
filtered_data.push(schools[i]);
filtered_data.push(row);
} else {
filtered_out.push(schools[i]);
filtered_out.push(row);
}
}
});
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;
_.map(filtered_data,function(row){
isOpen = row.ACTIVE.toUpperCase() == 'OPEN';
isPublic = (row.TYPE.toUpperCase() !== 'CHARTER' ||
row.TYPE.toUpperCase() !== 'PRIVATE');
meetsMinimumEnrollment = row.ENROLLMENT > minEnrollment;

// Constructing the styling options for our map
if (filtered_data[i].HAS_HIGH_SCHOOL){
if (row.HAS_HIGH_SCHOOL){
color = '#0000FF';
} else if (filtered_data[i].HAS_MIDDLE_SCHOOL) {
} else if (row.HAS_MIDDLE_SCHOOL) {
color = '#00FF00';
} else {
color = '##FF0000';
}
// The style options
var pathOpts = {'radius': filtered_data[i].ENROLLMENT / 30,
var pathOpts = {'radius': row.ENROLLMENT / 30,
'fillColor': color};
L.circleMarker([filtered_data[i].Y, filtered_data[i].X], pathOpts)
.bindPopup(filtered_data[i].FACILNAME_LABEL)
L.circleMarker([row.Y, row.X], pathOpts)
.bindPopup(row.FACILNAME_LABEL)
.addTo(map);
}
});

})();