Created
September 3, 2013 13:58
-
-
Save stongo/6424311 to your computer and use it in GitHub Desktop.
homework solution for M101JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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