Why does my FizzBuzz function say `test` is not a function?

0
1
Asked By MellowPine42 On

I'm trying to submit a recursive, pure-function-style solution for FizzBuzz, but the online judge reports `TypeError: test is not a function` at `arr[i - 1] = test(i)`. The same code appears to work when I run it locally. My code defines a `test` function and passes it into `fizzBuzz`, so I'm not sure why the judge cannot call it. Could the platform's expected function signature or parameters be affecting this?

3 Answers

Answered By CedarFox7 On

The likely problem is the function signature expected by the judge. The platform normally calls `fizzBuzz(n)`, but your function is declared as `fizzBuzz(i, n, test, arr)`. Since the judge supplies only one argument, your local `test` parameter is actually `undefined`, which causes the error when you call `test(i)`. Keep the required public signature and define the helper inside it, or use a simpler loop/map solution.

MellowPine42 -

That makes sense—I was testing it by passing all four arguments myself, while the judge only calls the exported function with `n`.

Answered By QuietMaple19 On

Naming both the outer helper and an inner parameter `test` is legal JavaScript, but it is confusing because the parameter shadows the outer variable. Rename the helper to something like `fizzBuzzValue` and the parameter to `formatter` if you keep that structure. The shadowing itself should not produce this error, though; an omitted argument from the judge is the more likely cause.

Answered By NovaHarbor3 On

A straightforward solution would be to have the submitted function accept only `n` and build the result directly: `const fizzBuzz = n => Array.from({length:n}, (_, index) => { const i=index+1; if (i%15===0) return 'FizzBuzz'; if (i%3===0) return 'Fizz'; if (i%5===0) return 'Buzz'; return String(i); });` This matches the usual judge interface and avoids manually passing an array and callback.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.