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
Binary file added .DS_Store
Binary file not shown.
Binary file added lab/.DS_Store
Binary file not shown.
40 changes: 33 additions & 7 deletions lab/lab1/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Instructions: "Write a function that adds one to the number provided"
Example: "plusOne(2) should return 3"
===================== */

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

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

Expand All @@ -17,7 +20,10 @@ 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) {
age = 2017 - birth;
return age;
};

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

Expand All @@ -26,7 +32,13 @@ 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(x) {
if (x > 9000){
return true;
}
else {return false;}

};

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

Expand All @@ -35,7 +47,11 @@ 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) {
var value = obj[key];
return value;

};

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

Expand All @@ -45,7 +61,10 @@ 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) {
y = (m*x)+b;
return y;
};

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

Expand All @@ -54,7 +73,14 @@ 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(params,x) {
var countItem = 0;
for (i=0; i<params.length; i++) {
if (params[i] == x){
countItem = countItem+1;
}
}
return countItem;
};

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

78 changes: 67 additions & 11 deletions lab/lab1/part2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@ 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) {
var x = a;
if (x%2 === 0){
return true;
}
else{
return false;
}

};

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

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

var justFalse = function() {};
var justFalse = function() {
if (isEven(justOne()) === true){
return true;
}
else{
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 === true){
return false;
}
else{
return true;
}
};

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

Expand All @@ -41,20 +67,49 @@ 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))==true){
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(params) {
var params2 = [];

for (i=0; i<params.length; i++) {
if (params[i] >10){
params2.push(params[i]);
}
}

return params2;
};
// 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) {
for (i=0; i<arr1.length; i++) {
if (arr1[i] ===arr2[i]){
count=1;
}
else count=0;
}
if (count==1){
return true;
}
}
console.log('filterOutLessThan10 success:',arraysEqual(filterOutLessThan10([4,11]),[11]));

/* =====================
Stretch goal
Expand All @@ -63,7 +118,8 @@ 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) {
};

console.log('filter success:', filter(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,[1] );

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 = _.difference(_.union(nathanGameList,jeffGameList),_.intersection(nathanGameList,jeffGameList));

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

var data = bikeArrayClean;

var largeStationList;
bikeCount = function(largestation){
var numBikes = largestation[3];
return numBikes>20;
};

var largeStationCount;

var largeStationList = _.filter(bikeArrayClean,bikeCount);

console.log(largeStationList.length);
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
56 changes: 56 additions & 0 deletions lab/lab2/part3-underscore-refactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,62 @@
// Mock user input
// Filter out according to these zip codes:
var acceptedZipcodes = [19106, 19107, 19124, 19111, 19118];
var input = '12345 - 123';
var split = '12345 - 123'.split(' ');
var firstSplit = split[0];
var firstSplitAsNumber = parseInt(firstSplit);

var fixZipCodes = function(x) {
if (typeof x == "string") {
var split = x.split(' ');
var firstSplit = split[0];
var firstSplitAsNumber = parseInt(firstSplit);
return firstSplitAsNumber;
} else {
return x;
}
};

_.map(['12345 - 123', 54321, '23412 - 342'], fixZipCodes);


var hasKindergarten = function(school) {
if (typeof school.GRADE_ORG == 'number') {
return school.GRADE_LEVEL < 1;
} else {
return school.GRADE_LEVEL.toUpperCase().indexOf('K') >= 0;
}
};

var hasKelementary = function(school) {
if (typeof school.GRADE_ORG == 'number') {
return school.GRADE_LEVEL < 6;
} else {
return school.GRADE_LEVEL.toUpperCase().indexOf('K') >= 0;
}
};

var hasmiddleschool = function(school) {
if (typeof school.GRADE_ORG == 'number') {
return school.GRADE_LEVEL < 9;
} else {
return school.GRADE_LEVEL.toUpperCase().indexOf('K') >= 0;
}
};


var hashighschool = function(school) {
if (typeof school.GRADE_ORG == 'number') {
return school.GRADE_LEVEL < 13;
} else {
return school.GRADE_LEVEL.toUpperCase().indexOf('K') >= 0;
}
};





// Filter according to enrollment that is greater than this variable:
var minEnrollment = 300;

Expand Down