3. ...And the good


#1

Probably something dumb but I can't see it
I keep getting "Oops, try again. It looks like you printed out the wrong number of items." Here is my code:

function FizzBuzz () {
for (var i=1; i<21; 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);
}
}
};
Can anyone help me please?
I will deploy appreciate it


#2

Have you considered how many should be printed and how many were printed? Seems like the next thing to do don't you think?


#3

Here's what i got. my only problem is it's not printing my fizz buzz and i can't figure out why.

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


#4

If you start reading from the top of the loop with i=15 and execute it in your head, do you reach "FizzBuzz"? If yes, then you've misunderstood something and will have to study what it does differently during those few lines, if no, then you've found where it goes wrong.


#5

I put the fizzbuzz part on top and mine now works.

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);
}
}


#6

There's another solution as well, where first Fizz is checked, and then Buzz - for 15 both would be true, and the result would be FizzBuzz (would just have to take care to not print newlines until both have been checked)


#7

Thanks so much for the quick replies everyone gave!
Ioniatan: I may have misunderstood a concept so I reviewed some of the past exercises of javascript and I still don't understand what you mean.
If you run the for loop the only "FizzBuzz" exception would be 15 so i express in the if/else statement so I don't really understand what I am missing here.
If you could walk me through a little bit into my mistake I would deeply appreciate it!
Thanks for taking your time to help me and so thanks to everyone else!


#8

I don't think for the exercise you're required to run a function. But if you want to run a function for the exercise, you should
a. make sure to call the function when you are done: for example, FizzBuzz();

b. you have a syntax error in you code

function FizzBuzz () {
for (var i=1; i<21; i++); {<---------------semi colons do not belong there
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);
}
}


#9

Thanks you unimog666 apparently y forgot to call the function at the end of the script plus that semicolon.


#10

try this:
for(var 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);
}
}


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.