Skip to content

Instantly share code, notes, and snippets.

@lazywithclass
Last active December 2, 2015 14:56
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 lazywithclass/506da0c924e1410bf512 to your computer and use it in GitHub Desktop.
Save lazywithclass/506da0c924e1410bf512 to your computer and use it in GitHub Desktop.
// variables
// control flow
// functions
// lamp exercise
var on;
function turnOn() {
if (!isOn()) {
on = true;
}
}
function turnOff() {
if (isOn()) {
on = false;
}
}
function isOn() {
return on;
}
// what if
// I try to turn on a lamp that's already on?
// Does it change for off?
// We want to model other operations? Could you think of any?
// elevator exercise
var currentFloor,
capacity,
people,
floors;
function goTo(floorNumber) {
if (floorNumber >= floors) {
currentFloor = floors;
} else if (floorNumber <= 0) {
currentFloor = 0;
} else {
currentFloor = floorNumber;
}
}
function loadPeople(quantity) {
if (people + quantity >= capacity) {
people = capacity;
} else {
people = people + quantity;
}
}
function unloadPeople(quantity) {
if (quantity >= people) {
people = 0;
} else {
people = people - quantity;
}
}
// What if
// You try to load more people than the capacity
// You try to unload more people than there are in the elevator?
// You try to go to a floor that does not exist?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment