Skip to content

Instantly share code, notes, and snippets.

@stongo
Created September 3, 2013 13:58
Show Gist options
  • Save stongo/6424311 to your computer and use it in GitHub Desktop.
Save stongo/6424311 to your computer and use it in GitHub Desktop.
homework solution for M101JS
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/school', function(err, db) {
if(err) throw err;
var _update = function(student) {
db.collection('students').update({_id: student._id}, student, function(err, saved) {
if (err) throw error;
console.log('Successfully updated student');
});
}
cursor = db.collection('students').find({});
cursor.each(function(err, student) {
var scores = student.scores;
function compare(a,b) {
if (a.score < b.score)
return -1;
if (a.score > b.score)
return 1;
return 0;
}
function homework(a) {
if (a.type == 'homework')
return -1;
else
return 1;
return 0;
}
scores.sort(compare);
scores.sort(homework);
if (scores[0].type == 'homework') scores.shift();
student.scores = scores;
_update(student);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment