Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Extra Credit - Fizz Buzz / Introduction to Programming

Hello, I just finished the extra credit challenge for the JavaScript Introduction to Programming course. I checked it and I think it works properly but I wanted to know If there's a way to optimize my code, any suggestions?

for (var counter = 1; counter <= 100; counter = counter + 1) {
   if (counter % 3 == 0 & counter % 5 == 0) {
      console.log("fizzbuzz"); }
   else {
      if (counter % 3 == 0) {
         console.log("fizz"); }
      else {
         if (counter % 5 == 0) {
            console.log("buzz"); }
         else {
         console.log(counter);}
      }
   }
}

3 Answers

Well, you can refactor your code a bit. Right now you are nesting your if statements inside the else statements. That makes your code harder to read. There's a shorthand syntax for that:

if (something) {
   // code
} else if (somethingElse) {
   // code
} else {
  // code
}

The if statements are still technically nested inside the else statements, but by putting them on the same line, you don't need the extra curly braces.

If you want to optimize your code a bit more, you could do something like this:

var output;
for (i = 1; i <= 100; i++) {
   output = "";
   if (i % 3 == 0) {
      output += "fizz"; // if i is divisible by 3 add 'fizz' to output
   }
   if (i % 5 == 0) {
      output += "buzz"; // if i is divisible by 5 add 'buzz' to output
   }
   if (output == '') {
      output += i; // if the string is empty add i to output
   }
   console.log(output);
}

In this code you end up making less checks and the fizzbuzz output is done by concatenating fizz with buzz if both checks are true.

If neither of the checks is true, the string will remain empty which means it's not divisible with 3 or 5, so we output the number instead.

in your first condition you use the single "&" whic is a Bitwise AND but you must use a logical AND using the double ampersand "&&"

Thanks Dino and Roberto! I'm just starting in JavaScript and I wasn't sure about the syntax of the if's and logical operators, I know a little Python and there are some slight differences. Dino, I liked your optimized solution, very clever! Roberto, what's a Bitwise AND?

Thanks guys!

[ from Wikipedia ] A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 and the second bit is 1; otherwise, the result is 0.