Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colevandersWands/5dafa5b93bd967d8aca7d366ea02a66e to your computer and use it in GitHub Desktop.
Save colevandersWands/5dafa5b93bd967d8aca7d366ea02a66e to your computer and use it in GitHub Desktop.
using model objects to manage bottles
const bottleManagement = {
bottles: {},
nextId: 0,
addBottle: function (newBottle){
newBottle.key = this.nextId
this.bottles[this.nextId] = newBottle;
this.nextId++;
return this.nextID - 1;
},
getBottle: function(id){
return this.bottles[id];
},
getAllBottles: function(){
return this.bottles;
},
updateBottles: function(bottleUpdate){
this.bottles[bottleUpdate.key] = bottleUpdate;
}
};
// things should be created in the scope they are modified
function Bottle(totalVol, currentVol, liquid){
return { totalVol, currentVol, liquid };
};
// using a named function expression
// instead of an anonymous function linked to a variable
function hydrationStatus(dailyIntake, remainingThirst){
return { dailyIntake, remainingThirst };
}
function drink (bottle, hydrationStatus){
hydrationStatus.currentVol = bottle.currentVol - thirst; // currentVol will be undefined
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;
}
let bottle = Bottle(1000, 600, "Water");
let new_id = bottleManagement.create(bottle)
bottle = bottleManagement.getBottle(new_id);
bottle = drink(bottle, 4);
bottleManagement.update(bottle);
// Please verify for:
// a) bug in addBottle - creating 2 bottles for Id 0,
// 2 console.log's
// b) coherence of code structure. (functions drink and fill still untested/not finished)
let bottleManagement = {
bottles: {},
nextId: 0,
addBottle: function (newBottle){
// move this out of bottleManagement.create
// things should be created in the scope they are modified
let Bottle = function (id, totalVol, currentVol, liquid){
this.id = id;
this.totalVol = totalVol;
this.currentVol = currentVol;
this.liquid = liquid;
};
// bottleManagement.bottles[this.nextId] - you had done this
// this reaches outside of the object to access bottles as a global variable
this.bottles[this.nextId] = newBottle;
this.nextId++;
},
getBottle: function(id){
return this.bottles[id];
},
getAllBottles: function(){
return this.bottles;
},
updateCurrentVol: function(id, newCurrentVol){
this.bottles[id].currentVol = newCurrentVol;
}
};
// let's use a factory instead of a constructor since you are not using inheritance
// and to build the habit of thinking in factories
// function Bottle(id, totalVol, currentVol, liquid) {
// return { id, totalVol, currentVol, liquid };
// };
let HydrationStatus = function (dailyIntake, remainingThirst){
this.dailyIntake = dailyIntake;
this.remainingThirst = remainingThirst;
}
// this function is redundant, it all it does is wrap the constructor you already wrote
function setHydrationStatus (dailyIntake, remainingThirst){
// using something not passed in as a variable
let hydrationStatus = new HydrationStatus (dailyIntake, remainingThirst);
return hydrationStatus;
}
function drink (bottle, thirst){
let newHydrationStatus; // declared a new variable, didn't create a newHydrationStatus
// and even if you did create one in here, you'd be using something that's not passed in
newHydrationStatus.currentVol = bottle.currentVol - thirst; // currentVol will be undefined
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