Skip to content

Instantly share code, notes, and snippets.

@Mariana88
Last active April 23, 2018 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mariana88/0acf28de1194c8bcbdcb382e30600a91 to your computer and use it in GitHub Desktop.
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)
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