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

Week3 Yufei Yuan #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Empty file added lab/lab1/.Rhistory
Empty file.
8 changes: 8 additions & 0 deletions lab/lab1/part1-array-access.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@
L.marker([0, 0]).addTo(map);
L.marker([0, 0]).addTo(map);

var header = ['lat','lng','label'];
var TempArr = [[39.952238, -75.201702,'Top1'],[39.952927, -75.217711,'Top2'],[39.944143, -75.174881,'Top3']];
TempArr.unshift(header);
for (i=0; i < TempArr.length; i++){console.log(TempArr[i])};
var makeAMarker = (obj) => {return L.marker([obj[0], obj[1]]).bindPopup(obj[2])}
for (var i=1; i <= TempArr.length; i++){ makeAMarker(TempArr[i]).addTo(map) }
Copy link
Member

Choose a reason for hiding this comment

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

<= is giving this error:

image

Try without the = it resolves the issue.

Copy link
Author

Choose a reason for hiding this comment

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

Changed. This solved the propblem!



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

End code
Expand Down
14 changes: 14 additions & 0 deletions lab/lab1/part2-fizzbuzz.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@

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

var num = []

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


/* =====================
Expand Down
32 changes: 26 additions & 6 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 @@ -70,9 +70,28 @@
Start code to filter data

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



//for clean data
Copy link
Member

Choose a reason for hiding this comment

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

Nothing appears on the map when I load the page:
part3-data-transformation.html

image

Does it work for you?

Copy link
Author

Choose a reason for hiding this comment

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

Changed. It should work now.

var dataFiltered = []
if (i=1,i<=data.length,i++){
if (data[i][3]>20){
dataFiltered.push(data[i])
}
}
console.log(dataFiltered);

/* working with dirty data
var data = bikeArrayDirty
var dataFiltered = []
var DataFilter = (dataArr) => {
for (let i=0; i<data.length; i++){
if (parseFloat(data[i][3]>20){
dataFiltered.push(data[i])
}
}
}
DataFilter(data);
console.log(dataFiltered)
*/
/* =====================

End code to filter data
Expand All @@ -98,7 +117,8 @@

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


var makeAMarker = (obj) => {return L.marker([obj[0], obj[1]]).bindPopup(obj[2]+obj[3]).openPopup()}
for (var i=0; i < dataFiltered.length; i++){ makeAMarker(dataFiltered[i]).addTo(map) }

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

Expand Down
34 changes: 31 additions & 3 deletions lab/lab1/part4-project-euler.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,37 @@
Start code

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



//problem 1
var numArr = []
for (let i = 1; i<1000; i++){
if (i%3===0){
numArr.push(i);
} else if (i%5===0){
numArr.push(i);
}
}
let numSum=0
for (let i=0; i<numArr.length; i++){
numSum=numSum+numArr[i];
}
console.log(numSum);

//problem 2
const arrSum = arr => arr.reduce((a,b) => a + b, 0)
var numArr2 = [1,2];
let x = 1;
let y = 2;
let evenSum = 2
while (arrSum(numArr2) < 4000000){
let z = x + y
if (z%2===0){
evenSum = evenSum + z;
}
numArr2.push(z);
x = y;
y = z;
}
console.log(evenSum);
Copy link
Member

Choose a reason for hiding this comment

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

You are close... But you didn't get the right answer...1089154..

Take a look: https://stackoverflow.com/questions/9053545/finding-the-sum-of-even-valued-terms-in-fibonacci-sequence

Copy link
Author

Choose a reason for hiding this comment

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

Changed

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

End code
Expand Down
Empty file added lab/lab2/.Rhistory
Empty file.
24 changes: 8 additions & 16 deletions lab/lab2/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,54 @@ 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(rossGameList, nathanGameList));
Copy link
Member

Choose a reason for hiding this comment

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

you are taking a union of two things that are the same.

Try switching the order of the inputs on one of the difference queries and see if the result changes.

Copy link
Author

Choose a reason for hiding this comment

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

Changed.

console.log('Which games are exclusive to one collection? In other words, only owned by either Ross or Nathan (but not both!).', query8);
59 changes: 58 additions & 1 deletion lab/lab2/part4-underscore-refactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@
schools[i].ZIPCODE = normalized_zip;
}

//refactor clean data
schools = _.each(schools, function(a){
if (_.isString(a.ZIPCODE)){
split = a.ZIPCODE.split(' ');
normalized_zip = parseInt(split[0]);
a.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;
schools[i].HAS_HIGH99_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;
Expand All @@ -56,6 +65,16 @@
}
}

//refactor typeof
schools = _.each(schools, function(b){
if (_.number(b.GRADE_ORG)){
b.HAS_KINDERGARTEN = b.GRADE_LEVEL < 1;
b.HAS_ELEMENTARY = 1 < b.GRADE_LEVEL < 6;
b.HAS_MIDDLE_SCHOOL = 5 < b.GRADE_LEVEL < 9;
b.HAS_HIGH99_SCHOOL = 8 < b.GRADE_LEVEL < 13;
}
});

// filter data
var filtered_data = [];
var filtered_out = [];
Expand Down Expand Up @@ -83,6 +102,26 @@
console.log('Included:', filtered_data.length);
console.log('Excluded:', filtered_out.length);

//refactor filter filter data
var filtered_data = _.filter(schools, function(c){
isOpen = c.ACTIVE.toUpperCase() == 'OPEN';
isPublic = (c.TYPE.toUpperCase() !== 'CHARTER' ||
c.TYPE.toUpperCase() !== 'PRIVATE');
isSchool = (c.HAS_KINDERGARTEN ||
c.HAS_ELEMENTARY ||
c.HAS_MIDDLE_SCHOOL ||
c.HAS_HIGH_SCHOOL);
meetsMinimumEnrollment = c.ENROLLMENT > minEnrollment;
meetsZipCondition = acceptedZipcodes.indexOf(c.ZIPCODE) >= 0;
filter_condition = (isOpen &&
isSchool &&
meetsMinimumEnrollment &&
!meetsZipCondition);
});
filtered_out = _.difference(school,filtered_out);
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++) {
Expand All @@ -108,3 +147,21 @@
}

})();

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