Last active
December 2, 2015 14:56
-
-
Save lazywithclass/506da0c924e1410bf512 to your computer and use it in GitHub Desktop.
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
// 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