34
tytho
8y

A programmer comes home from work. The spouse says, "Could you run to the store for me? Buy a gallon of milk, and if there are eggs, buy a dozen". He goes to the store and comes back with 13 gallons of milk.

buyMilk()
if (eggs) {
for (var i = 0; i < 12; i++) {
buyMilk()
}
}

Comments
  • 2
    This is a variation (he brings a dozen instead of 13.) and the code doesn't really make sense (incomplete)
    function buyMilk(){
    return (eggs)? 12 : 1;
    }
  • 1
    @xewl I like that version much better
  • 2
    @SoldierPeetaM the for loop definition is split up into the parts. Before the first ";" is the initiator, which only gets run once before the loop is run. The piece after the first ";" is the comparator, which is the condition that will be run at the beginning each iteration of the loop to check if the loop should keep running (to prevent infinite loops). The last section after the second ";" is called something else 😜 and is used to change variables that will affect the condition. In this case it takes the variable "i" which begins at 0, and increments by 1 at the end of each iteration ("i++" is the same as "i = i + 1").
  • 0
    @tytho oh right, my bad, thanks for telling me!
Add Comment