var count = 0;
function goFast(){
++count;
console.log("Go straight and then ");
var direction = "right"; if (count%2===0){direction="left";}
turn(direction);
console.log("Thank you passenger #" + count);
}
var turn = function(direction) {
console.log("turn to your " + direction)
}
goFast();
goFast();
The goFast function is in charge of counting how many travelers pass through, and asks another function where they should turn (which logs out left or right alternatively).
How can I bring my count variable inside the goFast function and thus better encapsulate it, but without re-initializing it every time the function is invoked of course?
Here is a jsfiddle: http://jsfiddle.net/legolandbridge/sU8T8/