Comment on A non-programmer’s solution to “Fizz Buzz”parentComments−metaxy213yInspired by this, I translated the whole solution into JS. I renamed your "query" object to "output_state," which seems like a more accurate name. var output_state = {}; function print(x) { console.log(x); if (typeof x === "number") { output_state = {last_num: x, time_since: 1}; } else { output_state.time_since += 1; } } function fizzbuzz() { switch(output_state.last_num) { case undefined: print(1); break; default: var a = output_state.last_num; var b = output_state.time_since; var number = a + b; if (number % 5 !== 0) { if (number % 3 === 0) { print("Fizz"); } else { print(number); } } else { if (number % 3 === 0) { print("FizzBuzz"); } else { print("Buzz"); } } } fizzbuzz(); // Or, correctly: if (number < 100) { fizzbuzz(); } }
Comments
Inspired by this, I translated the whole solution into JS. I renamed your "query" object to "output_state," which seems like a more accurate name.