3/33 Oops, try again. It looks like you printed out the wrong number of items


#1

I can't seem to figure this out. Here is my code!

var numbers= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];

for(var numbers=0; numbers<21; numbers++) {

if (numbers%3===0) { console.log("Fizz");
};

if(numbers%5===0) {console.log("Buzz");
};

if(numbers%3===0&&numbers%5===0) { console.log("FizzBuzz"); }

else {console.log(numbers);
}
};


#2

Your code first creates a numbers array (which is not needed) but then the for statement overwrites that variable when it declares and initializes it to 0

for (var numbers = 0; ...){}

You are on the right track, though. The first line (the array) may be removed. Let's simplify the iterator variable so the loop is easier to read:

for (var i = 1; i < 21; i++) {
    // if statements
}

The order in which we test the iterator value is important. If we test only against 3 or 5 before we test against 3 AND 5, we will never write FizzBuzz.

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

#3

Thank you so much!!

Worked great. and thank you for explaining. I was getting very frustrated and discouraged


#4

Logic and control flow are two very complex subjects, on the whole, but they both trace back to simple decisions, in many cases binary, either..or, and in others what I like to call multi-way, or n-ary such as above where there are multiple conditions to test against.

I'll grant the logic can sometimes be frustrating, and can offer a bit of condolence in the form of a little stick of wood and graphite called a pencil. We can trace our entire program in diagram form with a pencil and paper. Work it out on paper, and translate to code algorithm. When debugging, do the reverse... Translate the code algo back to a pencil diagram. The flaws will jump off the paper, in many instances.

You will be amazed with how fast you begin to visualize program flow and logic. How we might illustrate our logic is up to us, but we do have a standard to fall back on, if we wish... The flow chart. It's symbols are adopted and documented and everywhere to be found.

After a time we can develop our own intuitive form of illustration. I use a shoelace forming a circle with one intersection of the string to represent a function. The bottom of the the hoop is the callee, and the intersection is the caller. For scope I use a tree diagram. A lot of my pencil work is just dots and lines.

Today, it is truly just in my head, but it took using a pencil for many years to get here. Get out the pencil and paper and devise a way to utilize to the fullest in helping you to write and test all manner of algorithm. It's like training wheels for programmers, and more, a teaching tool that never gets old or discarded. We never throw away our pencil.


#6

Thank you all! I got through this. Again thanks, and I have no idea how else to say I have bypassed this assignment.