Why does my FizzBuzz function say that the callback is not a function?

September 18, 2026 2 answers
Asked by ladder-rung-8
I'm trying to solve FizzBuzz with a recursive, pure-function-style approach. My local test produces the expected output, but the online judge reports `TypeError: test is not a function` at `arr[i - 1] = test(i)`. The callback is declared and passed into `fizzBuzz`, so I'm not sure why the judge cannot call it. Here is the relevant code: ```js const test = (i) => { if (i % 5 === 0 && i % 3 === 0) return "FizzBuzz"; if (i % 5 === 0) return "Buzz"; if (i % 3 === 0) return "Fizz"; if (i % 3) return i + ""; }; const fizzBuzz = (i, n, test, arr) => { arr[i - 1] = test(i); if (i !== n) fizzBuzz(i + 1, n, test, arr); return arr; }; ``` It works when I call it myself with all four arguments, but fails when submitted to the judge.

Answers

robot vacuum woes
Try implementing the required `fizzBuzz(n)` entry point and create the array and callback inside it. For example: ```js var fizzBuzz = function(n) { const result = []; for (let i = 1; i <= n; i++) { if (i % 15 === 0) result.push("FizzBuzz"); else if (i % 3 === 0) result.push("Fizz"); else if (i % 5 === 0) result.push("Buzz"); else result.push(String(i)); } return result; }; ``` Also make sure the non-multiple case always returns a string. Your original `if (i % 3)` happens to work for numbers that are not divisible by three, but an unconditional final `return String(i)` is clearer and safer.
Reply to this answer
Whisk attachment!
The issue is probably the required function signature, not the callback itself. The judge calls your submitted function with only one argument, `n`, because that is the expected FizzBuzz interface. In your version, that value becomes `i`, while `n`, `test`, and `arr` are `undefined`. Consequently, `test(i)` tries to call `undefined` as a function. Keep helper functions inside the submitted function or call your recursive function with all of its required arguments from a wrapper matching the platformโ€™s expected signature.

brick kiln: The parameter name `test` shadows the outer constant with the same name, but JavaScript permits that and it works when all four arguments are supplied. Renaming it to something like `formatter` would make the code clearer, but it does not explain the judge error by itself.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions