Track Temps
by Ryan Morris
JavaScript
// Use an IFFEE to create a module
// Our module will be responsible for tracking the history
// of temperature values that we pass it
//
// Ex usage:
// Temp.setTemp(50);
// Temp.setTemp(20);
// Temp.getTemp(); // 20
//
// The module should have four methods
// setTemp(temp)
// Expects one numeric argument ,the temp to store
// Returns the temp it stored
// getTemp(i:optional)
// Expects none or one argument, i
// Returns the latest stored temp when no arguments given
// Returns the temp "i back" when i is provided
// clear()
// Expects no arguments
// Completely clears the temperatures stored
//
// Bonus:
// Track a timestamp with each temperature when it is stored
// When reporting back the temp (getTemp) return a string:
// "50 degrees as of 12:30pm May 15th 2017
//
var Temp = (function() {
var temps = [];
return {
setTemp: function(tempurature) {
temps.push(tempurature);
},
getTemp: function(i) {
return i ? temps[i-1] : temps[temps.length-1];
},
clear: function() {
temps = [];
}
}
})();
Temp.setTemp(5);
Temp.setTemp(10);
Temp.setTemp(12);
Temp.setTemp(15);
console.assert(Temp.getTemp() === 15, "Latest is 15");
console.assert(Temp.getTemp(1) === 5, "First is 5");
console.assert(Temp.getTemp(2) === 10, "Second is 10");