Last active
January 21, 2016 10:01
-
-
Save kosmikko/1c3a467cbd5934ad37b7 to your computer and use it in GitHub Desktop.
Credits transfer (find issues with it)
This file contains 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
function transferCredits(from, to, amt) { | |
var fromAccount = db.game_accounts.findOne({"name": from},{"credits": 1}); | |
var toAccount = db.game_accounts.findOne({"name": to},{"credits": 1}); | |
db.game_accounts.update({name: from}, {$set: {credits: fromAccount.credits - amt}}); | |
db.game_accounts.update({name: to}, {$set: {credits: toAccount.credits + amt}}); | |
} | |
db.game_accounts.insert({name: "John", credits: 1000}); | |
db.game_accounts.insert({name: "Jane", credits: 1000}); | |
// John transfers credits to Jane | |
transferCredits("John", "Jane", 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment