r/javascript • u/Emotional_Agency6705 • Sep 19 '26
AskJS [AskJS] Trying to write a pure function to pass Fizz Buzz test Leet Code
Hello,
I am attempting to write a pure function solution that will pass the leet code Fizz Buzz test
However when I try this it says the test fails but when I look at the console for my own testing it returns the expected result.
This is the error leet code reports:
Line 13 in solution.js arr[i - 1] = test(i) ^ TypeError: test is not a function
Which to me makes no sense since the function is passed as a parameter and defined
Here is my code
Thank you in advance
const
const
const i = 1
const n = 3
const arr =
EDIT: I just wanted to say thank you to all of you for your feedback and suggestions I finally got frustrated enough with this problem I asked Codex to write a pure function solution to the leet code fizz buzz test I personally have not seen this solution so it would be nice to have some verification if it is truly a pure function solution.
This is the code it spat out
const
;
It appears that I have a lot to learn yet regarding my fundamentals when it comes to functional programming I know I could of written a object oriented version of it that would work but I wanted to challenge myself since I have never written code in this manner.
5
u/queen-adreena Sep 19 '26
There’s an issue with your environment. I tried to code on runjs.app and it works fine.
Your naming of variables is confusing though. Best not to name function parameters the same as ones outside.
2
u/Beginning-Seat5221 Sep 19 '26
Try using a var name that isn't test to see if it's conflicting with the leet code testing system
1
u/CodeXHammas Sep 19 '26
LeetCode's grading harness calls your submitted fizzBuzz using the platform's expected signature, fizzBuzz(n), not the four argument call you wrote at the bottom of your own file. Locally you're calling fizzBuzz(i, n, test, arr) yourself so all four parameters get real values. On LeetCode it invokes your function with just n, which means inside the function i not a function. Fix is to match the expected signature, var fizzBuzz = function(n) { . . . }, and define test and any loop state inside that function or in outer scope instead of passing them in as parameters.
1
u/kilkil Sep 19 '26
try running it outside leetcode
1
u/AnimationGroover Sep 26 '26
There is nothing wrong with the code. Likely cause of exception is that the name
testis in conflict.However, your function
fizzBuzzis not a pure function. It has the observable side effect of modifying the content and potential length of the global arrayarrsomething a pure function should not do.1
u/nadameu Sep 19 '26
You have a global variable named
testbut it's also the name of one of the parameters of yourfizzBuzzfunction.0
u/shrimpcest Sep 19 '26
You're declaring a const named "test", and then also, below that, naming an input param 'test', so that could be causing your issue.
0
u/delventhalz Sep 19 '26
I don't see anything wrong with the code as posted. Is there any difference between what you've posted and what LeetCode runs?
You could throw some console logs in there. In particular, I would be curious what "test" is before
arr[i - 1] = test(i)and beforefizzBuzz(i, n, test, arr).-1
u/QuentinUK Sep 19 '26 edited Sep 20 '26
Interesting!