## DEV Community is a community of 861,926 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: FizzBuzz JavaScript

Akhil • Edited on

I think you made a small typo in for loop, for( i = 1;i<=n ; i++).
I was asked this same question and I gave the exact answer. The answer has one disadvantage to it, if the interviewer asks to modify question such that,

if the current number is divisible by 7 print foo
if its divisible by 11 print bazz
if its divisible by 7 and 11 print foobazz
if its divisible by 3 and 7 print fizzfoo
if its divisible by 3 and 5 and 7 print fizzbuzzfoo

the above approach doesn't work well in these conditions since now we have to maintain multiple conditions, instead do this.

``````let str = "";
for( i = 1;i<=n ; i++){
if(n%3 == 0) str += "fizz";
if(n%5 == 0) str += "buzz";
if(n%7 == 0) str += "foo";
if(n%11 == 0) str += "bazz";
else str += n;
}
``````

Hey Akhil,
Thanks for pointing out. I was approaching to this specific challenge with 3 and 5 but great to know this! Thanks for sharing!

JP Antunes

Mind you that with that approach every n is evaluated 4 times before a result sting is printed , so it's less efficient than Tadea's solution.

Akhil • Edited on

Nope, it would be the same since here I have modified the question for 4 conditions, ie 3,5,7,11. If we follow the original question ie checking for 3 & 5, then code will be :

``````let str = "";
for( i = 1;i<=n ; i++){
if(n%3 == 0) str += "fizz";
if(n%5 == 0) str += "buzz";
else str += n;
}
return str;
}
``````
JP Antunes • Edited on

Interesting. What do you think happens after each if statement?
Or in another way, what is the difference between two or more if's and a chain of if, else if, else?

edit: a picture of my cat doing a code review :-)

``````if(str ="") str += n;