“A problem well stated is a problem half solved.” --John Dewey
Well, FizzBuzz challenge is a classic test given in coding interviews. I need to write a function call FizzBuzz. The task is simple:
When you pass a number to function, it calls out "Fizz" if the number is divisible by 3,
calls out "Buzz" if the number is divisible by 5,
and calls out "FizzBuzz" if the number is divisible by both 3 AND 5.
Else, the function returns the number.
I've completed the code as below. However, the test kept displaying an error: "FizzBuzz(7) should be 7! ""
Function fizzuzz(num) {
*for(var i=0;i<=num;i++){
if(i%3===0 && i%5===0){console.log("FizzBuzz");}
else if(i%3===0) {console.log("Fizz");}
else if(i%5===0) {console.log("Buzz");}
else{console.log(i);}}*/
if(num%3===0 && num%5===0) {console.log("FizzBuzz");}
else if(num%3===0) {console.log("Fizz");}
else if(num%5===0) {console.log("Buzz");}
else {console.log(num);} }
fizzBuzz(7);
I got stuck on this issue for over 3 hours... simply because i misread the expectation of challenge.
I thought that fizzbuzz(7) should returns a whole array of [1, 2, Fizz, 4, Buzz, 7].
I've pseudocode and console.logging and my function seems to work fine. i still didn't get why i didn't pass the test
I even thought that there was something wrong with the test function so i went on slack to see if anyone else has the same problem like me. Unfortunately, no one seems to have any propblem with the test. I was too shy to ask for help on Slack because i was so behind compare to my coding mates.
Eventually, i kinda used the rubber ducky method to get me unstuck (FYI: I didn't even know what is the rubber ducky method before i did the challenge) I asked myself (a.k.a Mrs. Rubber Ducky): "" what do i need to do so that anytime i call the function fizzBuzz(7), it returns 7 only?" Then i changed the code and woohoo! it passed!
I feel happy when i got it at the end. Along the journey, i also feel like an idiot for overthinking and not paying attention to the expectation of the outcome in the test.