Last active
April 23, 2018 13:26
-
-
Save Mariana88/0acf28de1194c8bcbdcb382e30600a91 to your computer and use it in GitHub Desktop.
Please verify for: a) bug in addBottle - creating 2 bottles for Id 0, b) coherence of code structure. (functions drink and fill still untested/not finished)
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
let bottleManagement = { | |
bottles: {}, | |
nextId: 0, | |
addBottle: function (totalVol, currentVol, liquid){ | |
let Bottle = function (id, totalVol, currentVol, liquid){ | |
this.id = id; | |
this.totalVol = totalVol; | |
this.currentVol = currentVol; | |
this.liquid = liquid; | |
}; | |
bottleManagement.bottles[this.nextId] = new Bottle (this.nextId, totalVol, currentVol,liquid); | |
this.nextId++; | |
}, | |
getBottle: function(id){ | |
return bottleManagement.bottles[id]; | |
}, | |
getAllBottles: function(){ | |
return bottleManagement.bottles; | |
}, | |
updateCurrentVol: function(id, newCurrentVol){ | |
bottleManagement.bottles[id].currentVol = newCurrentVol; | |
} | |
}; | |
let HydrationStatus = function (dailyIntake, remainingThirst){ | |
this.dailyIntake = dailyIntake; | |
this.remainingThirst = remainingThirst; | |
} | |
function setHydrationStatus (dailyIntake, remainingThirst){ | |
let hydrationStatus = new HydrationStatus (dailyIntake, remainingThirst); | |
return hydrationStatus; | |
} | |
function drink (bottle, thirst){ | |
let newHydrationStatus; | |
newHydrationStatus.currentVol = bottle.currentVol - thirst; | |
if (thirst > bottle.currentVol){ | |
newHydrationStatus.remainingThirst = thirst - bottle.currentVol; | |
} else { | |
newHydrationSstatus.remainingThirst = 0; | |
} | |
return newHydrationStatus; | |
} | |
function fill (bottle, refill){ | |
let newCurrentVol; | |
let leftOver = refill - (bottle.totalVol - bottle.currentVol); | |
if (leftover > 0){ | |
alert("Can only fill this bottle up to " + leftover + "ml!") | |
} else { | |
newCurrentVol = bottle.currentVol + refill; | |
} | |
return newCurrentVol; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment