3 ms·
What I expect to see as an interviewer: for (let i = 1; i <= 100; i++) { let output = ""; // test modulos in ascending order if (i % 3 === 0) out
by gregjor 7y ago
What I expect to see as an interviewer:
for (let i = 1; i <= 100; i++) {
let output = "";
// test modulos in ascending order
if (i % 3 === 0) output += "Fizz";
if (i % 5 === 0) output += "Buzz";
if (output === "") output += i;
console.log(output);
}
Look Ma, I can maintain it!
for (let i = 1; i <= 100; i++) {
let output = "";
// test modulos in ascending order
if (i % 3 === 0) output += "Fizz";
if (i % 5 === 0) output += "Buzz";
if (i % 7 === 0) output += "Foo";
if (i % 11 === 0) output += "Bar";
if (output === "") output += i;
console.log(output);
}
Since I didn't introduce any new functions like isMultiple as a unnecessary shorthand for the built-in modulo operator, and I didn't introduce unnecessary arrays to iterate over the index of, I don't need to write a test suite. I can simply compare the output to the expected results.
Not having the function call for every value will help this "scale" if necessary, though for this problem scaling was neither stated as a requirement, nor implied by any reasonable reading of the requirements.
Putting on my hiring manager hat, if a candidate changes a for loop to a map() because "there are just a few programming languages that doesn’t support loops," I would ask the candidate to name some of those languages, and how one could solve a problem like FizzBuzz without loops. I would expect to talk about recursion and declarative languages (Prolog, or even SQL), not about `map()`.