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

Yang Wang Week3 #14

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
28 changes: 25 additions & 3 deletions lab/lab1/part1-array-access.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,31 @@

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

L.marker([0, 0]).addTo(map);
L.marker([0, 0]).addTo(map);
L.marker([0, 0]).addTo(map);
let Restaurant1 = [40.034508187448395,-75.16472099534634,'Chinese'];
let Restaurant2 = [40.06738292573648,-75.17898120530263,'Japanese'];
let Restaurant3 = [39.967025897357665,-75.16141103997865,'American'];

let restaurantData = [Restaurant1,Restaurant2,Restaurant3];

var icon=L.icon({
iconUrl:'images/marker-icon.png',
iconSize: [32,38],
});

restaurantData.push(
[39.9530635609646,-75.18387328197538,'Spanish'],
[39.97183994499194,-75.16276471850303,'Italian']
);

var addMarkers = function(map){
for (var i=0; i<restaurantData.length; i=i+1){
L.marker([restaurantData[i][0], restaurantData[i][1], {icon:icon}])
.addTo(map)
.bindPopup(restaurantData[i][2]);
}
};

addMarkers(map);

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

Expand Down
12 changes: 11 additions & 1 deletion lab/lab1/part2-fizzbuzz.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@

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


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

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

Expand Down
52 changes: 48 additions & 4 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 @@ -63,15 +63,46 @@

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

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

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

Start code to filter data

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


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

//dirty data
var dataFiltered = [];
for (var 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 All @@ -98,7 +129,20 @@

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

var icon = L.icon({
iconUrl:'images/marker-icon.png',
iconSize: [32,38]
});

var addMarkers = function(map){
for (var j=0; j<dataFiltered.length; j++) {
L.marker([dataFiltered[j][0], dataFiltered[j][1]], {icon:icon})
.addTo(map)
.bindPopup(dataFiltered[j][2] + ',' + dataFiltered[j][3]);
}
};

addMarkers(map);

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

Expand Down
42 changes: 41 additions & 1 deletion lab/lab1/part4-project-euler.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,47 @@

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


/*problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we
get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.*/

var array = [];
let sum = 0;
for(var i=0; i<1000; i++) {
if (i%3 === 0 || i%5 === 0) {
array.push(i);
sum += i;
}
};

console.log(sum);

/*problem 2
Each new term in the Fibonacci sequence is generated by adding the previous
two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not
exceed four million, find the sum of the even-valued terms.*/

let evenSum = 0;
let fibArray = [1,2];
var x=0;
var y=0;

while (fibArray[0] < 4000000) {
for (var j=0; j<1; j++) {
if (fibArray[j]%2 === 0) {
evenSum += fibArray[j];
}
}
x=fibArray[0];
y=fibArray[1];
fibArray[0]=x+y;
fibArray[1]=fibArray[0]+y;
};
Copy link
Member

Choose a reason for hiding this comment

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

the correct answer is: 4613732. You got: 881144
according to


console.log(evenSum);

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

Expand Down
16 changes: 8 additions & 8 deletions lab/lab2/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,61 @@ 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 = _.last(rossGameList, rossGameList.length-1);
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);
74 changes: 74 additions & 0 deletions lab/lab2/part4-underscore-refactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,34 @@
schools[i].ZIPCODE = normalized_zip;
}

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

// Check out the use of typeof here — this was not a contrived example.
// Someone actually messed up the data entry
schools = _.each(schools, function(grade) {
if (_.isNumber(grade.GRADE_ORG)) {
grade.HAS_KINDERGARTEN = grade.GRADE_LEVEL < 1;
grade.HAS_ELEMENTARY = 1 < grade.GRADE_LEVEL < 6;
grade.HAS_MIDDLE_SCHOOL = 5 < grade.GRADE_LEVEL < 9;
grade.HAS_HIGH_SCHOOL = 8 < grade.GRADE_LEVEL < 13;
} else {
grade.HAS_KINDERGARTEN = grade.GRADE_LEVEL.toUpperCase().indexOf('K') >= 0;
grade.HAS_ELEMENTARY = grade.GRADE_LEVEL.toUpperCase().indexOf('ELEM') >= 0;
grade.HAS_MIDDLE_SCHOOL = grade.GRADE_LEVEL.toUpperCase().indexOf('MID') >= 0;
grade.HAS_HIGH_SCHOOL = grade.GRADE_LEVEL.toUpperCase().indexOf('HIGH') >= 0;
}
}
);

//old code
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;
Expand All @@ -56,6 +82,7 @@
}
}


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

//refactored filter data
var filtered_data = _.filter(schools, function(filter) {
isOpen = filter.ACTIVE.toUpperCase() == 'OPEN';
isPublic = (filter.TYPE.toUpperCase() !== 'CHARTER' ||
filter.TYPE.toUpperCase() !== 'PRIVATE');
isSchool = (filter.HAS_KINDERGARTEN ||
filter.HAS_ELEMENTARY ||
filter.HAS_MIDDLE_SCHOOL ||
filter.HAS_HIGH_SCHOOL);
meetsMinimumEnrollment = filter.ENROLLMENT > minEnrollment;
meetsZipCondition = acceptedZipcodes.indexOf(filter.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);

// main loop
var color;
for (var i = 0; i < filtered_data.length - 1; i++) {
Expand All @@ -108,3 +159,26 @@
}

})();

//refactored main loop
_.each(filtered_data, function(color) {
isOpen = color.ACTIVE.toUpperCase() == 'OPEN';
isPublic = (color.TYPE.toUpperCase() !== 'CHARTER' ||
color.TYPE.toUpperCase() !== 'PRIVATE');
meetsMinimumEnrollment = color.ENROLLMENT > minEnrollment;

if (color.HAS_HIGH_SCHOOL){
color = '#0000FF';
} else if (color.HAS_MIDDLE_SCHOOL) {
color = '#00FF00';
} else {
color = '##FF0000';
}

var pathOpts = {'radius': color.ENROLLMENT / 30,
'fillcolor': color};
L.circleMarker([color.Y, color.X], pathOpts)
.bindPopup(color.FACILNAME_LABEL)
.addTo(map);
}
);