Social Icons

Pages

Wednesday, November 30, 2016

Introduction to Objects I 3/33

And the good!
We're going to play a game of FizzBuzz. The rules are simple. We want to count from 1 to 20. But if the number is divisible by 3, we're going to print "Fizz". And if the number is divisible by 5 we're going to print "Buzz".
What will we print if the number is divisible by 3 AND 5? That's right! "FizzBuzz"!
There are many ways to do this, but we'd like you to use a nested conditional in this exercise.

Instructions
  1. Print out the numbers from 1 - 20.
  2. The rules:
    • For numbers divisible by 3, print out "Fizz".
    • For numbers divisible by 5, print out "Buzz".
    • For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
    • Otherwise, just print out the number.
?
Hint
  1. Use a for loop to iterate through the numbers 1 to 20.
  2. Use an if / else to check if the number is divisible by 3 or 5
  3. Log to the console either "Fizz","Buzz", or "FizzBuzz".
Here is how to use an if/else statement:
if(condition) {
    // Do one thing here
}
else {
    // Do another thing here
}

                                                      for(i=1;i<21;i++)
                                                       {
                                                          if(i%3==0&&i%5==0){console.log("FizzBuzz");}
                                                               else if (i%5==0){console.log("Buzz");}
                                                                   else if (i%3==0){console.log("Fizz");}
                                                                      else{console.log(i);}

                                                       };



No comments:

Post a Comment