About coding the“FizzBuzz” interview question
At first reading I thought the author intended satire.<p>The first two code examples meet the stated requirements, except for an off-by-one error in both:<p><pre><code>  for (let i = 1; i &lt; 100; i++) { ... }
</code></pre>
Requirement: Print the numbers from 1 to 100. Oops.<p>The second example confusingly appends to the <i>output</i> variable -- except for the case of outputting the number --  although that variable gets set to empty on every iteration of the loop, and only one if&#x2F;else block will get executed. Minus one for clarity.<p>Then the author goes off the rails and keeps going.<p><i>But there is still a big problem about maintainability.
What if we want to add more control structures? Let’s say we want to print “Foo” when a number is multiple of 7 and “Bar” when it’s multiple of 11...</i><p>Possible future requirements are not called &quot;maintainability.&quot; The author gives a good example of YAGNI, though. Then after calling out this supposed problem the author does not give an example of how to address the possible proliferation of if&#x2F;else conditions, even after calling it a &quot;bad, bad, bad idea.&quot; Instead he shows an even less idiomatic and less clear <i>switch</i> construction that <i>has exactly the same problem</i>, with even more possibility of error due to leaving out a <i>break</i> by accident. The supposed maintainability problem -- what if we have to deal with 7 and 11 in the future -- never gets addressed.<p>Then it gets worse. To avoid the for loop, which has one local variable, the author creates an array merely to iterate over it. Let&#x27;s think about how that scales to doing FizzBuzz from one to ten million, since the author brought up scalability (although that wasn&#x27;t an original requirement).<p><i>But now… let’s make the things harder. How would you approach the same problem without using loops?</i><p>The problem explicitly describes a loop from 1 to 100. But...<p><i>That is a tricky question and will show the candidate deep knowledge of that particular programming language. I mean, there are just a few programming languages that doesn’t support loops, so we’re all pretty addicted to them!</i><p>Sorry, <i>map()</i> is just a loop, a loop with function call overhead.<p>This code has another off-by-one error, apparent if you just run the code in the browser. It assigns the local variable <i>num</i> to <i>i + 1</i>, since array indices begin at 0, but uses <i>i</i> in all of the calls to <i>isMultiple()</i> when it should use <i>num</i>. Running the code I get FizzBuzz as the very first output line. Note that the <i>for</i> loop solution didn&#x27;t have this problem, but now the details of Javascript array indexes have surfaced into the code and become a new problem to think about.<p>This bug -- using <i>i</i> when the parameter is named <i>num</i> -- carries into the final example, which throws an error right off. That tells me the programmer didn&#x27;t test this code, or even read through it carefully.<p>Looking at the test suite I see that it doesn&#x27;t check for boundary conditions, like passing 0 to <i>isMultiple()</i>. I would want to discuss what <i>isMultiple()</i> should do when passed a 0.<p>At this point the code bears almost no resemblance to the original problem statement, so as a hiring manager I would give a C- for a whiteboard exercise, and an F for a published article. I&#x27;ve seen several off-by-one and variable naming errors in the iterations, which looks careless, especially for code I would expect the programmer to write iteratively in a JS console. I&#x27;ve seen simple code balloon into something complex and unreadable, and I can&#x27;t just copy&#x2F;paste it into a Javascript console anymore.<p>I wanted to see if the candidate can code a simple problem <i>at all</i>, not for a demonstration of increasingly obscure techniques and scope creep. This programmer may know Javascript, but they are going to drop spanners into every task I assign, and waste other team members&#x27; time polishing turds like this.