Fizz buzz debug


#1

Not sure if it's proper form to repost the same question as a new post like so, or if it should be posted within an existing one as a reply.

Anyway, I could really use another set of eyes.

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

for(var i =0;i < array.length;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(array[i]);
    }
}

Degug efforts that have failed-
-I have tried this syntax for the Fizzbuzz line but it does not work either-

((i % 3 === 0) && (i % 5 === 0))

-ending loop at i <= 20 instead of array.length
-reversing the order of the last two conditionals
-replace all === with == .
-printing loop alone to console to isolate problem
The problem is I cannot make it past the first conditional without an error.
Console:

FizzBuzz
2
3
Fizz
5
Buzz
Fizz
8
9
Fizz
Buzz
12
Fizz
14
15
FizzBuzz
17
18
Fizz
20

Error messages:

Oops, try again. You printed FizzBuzz when you should have printed 1
``` 
```
Oops, try again. It looks like you printed out the wrong number of items.

#2

Maybe you could try to see first if number from array is not evenly divisible by those numbers and the check for others.

if(i % 3 !== 0 && i % 5 !== 0){}

same for the rest of the cases.


#3

I've tried this in an outside text editor and the error persisted. This is all for an outside editor.

I didn't understand your suggestion so I took a step back and I'm getting something quite wrong. I'm stumped. Am I missing something fundamental? I must be.

How are we supposed to declare:

Print out the numbers from 1 - 20.

I think this might be the problem, no?

for(var i =0;i < array.length;i++){
  if (i % 3 === 0){
  console.log("Fizz");
  } else if (i % 5 === 0) {
   console.log("Buzz");
  } else {
   console.log(array[i]);
  }
};

Fizz
2
3
Fizz
5
Buzz
Fizz
8
9
Fizz
Buzz
12
Fizz
14
15
Fizz
17
18
Fizz
20

#4

would be:

for(var i = 1; i <= 20; i++){
	console.log(i);
}

#5

I see. Well I removed the array and added that code. I have this now, but I get the exact same printout as before.

for(var i =0;i <= 20;i++){
  if (i % 3 === 0){
  console.log("Fizz");
  } else if (i % 5 === 0) {
   console.log("Buzz");
  } else {
   console.log(i);
  }
};

I'm trying to get only numbers that are divisible by 3, or by 5 to print the words. But as is shown, random numbers like 1 and 19 are being printed.


#6

I see U stared at an index of one. That seems to fix the problem. Why is that?

EDIT: I see. I must have thought this was the index of 1, not the value 1. I was starting at the value zero. This caused more massive problems than I thought.

I'm don't get why it goes so wonky when U start at 0, since the conditions should still hold true no? I guess the logic is out of my league.

Thanks for replies


#7

Oh yeah. You are not doing array[i] that loops over places in array. You are just looping over a pure numbers that go from 1 to 20, outputing the numbers in that range