3/8 ...and the good. It looks like you printed out the wrong number of items... I can't see the problem!


#1

My output is correct... all of it! The Fizz, Buzz and FizzBuzz. It prints numbers 1-20 inclusive...

I have looked at different code and solutions but what I see either doesn't apply or doesn't make sense.

Why x=1 and not x=0.

The array starts at zero and 1 is the first number. Why would I specify that the first number is the number 2?

I am getting quite frustrated with this exercise because it doesn't make sense.

Yes, I have refreshed the browser.

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


for (y = 1; y < 20; y++){
````if ((x[y] % 3 === 0) && (x[y] % 5 === 0)) {
````````console.log("FizzBuzz")}
````else if (x[y] % 5 === 0) {
````````console.log("Buzz")}
````else if (x[y] % 3 === 0) {
````````console.log("Fizz")}
````else {
````````console.log(x[y])}

};
  

Any help is appreciated!

Dr. K


#2

@dr.kung-fucocopops
Try it by changing the FOR-loop condition
from your

into
y < x.length

With
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
you created a numbers Array.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

To get the 1st Array-Element of the numbers Array
you use
numbers[0]

and to get all Elements with a for-loop you will have to start
at Element-zero
like

for (var x = 0; x < numbers.length ; x++ ) {
   console.log( number[x] );
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

In
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch


#3

You can try this solution:
for (var i = 1; i<=20; i++)
{
if(i%3 === 0 && i%5 !== 0)
{
console.log("Fizz");
}
else if(i%5 === 0 && i%3 !== 0)
{
console.log("Buzz");
}
else if(i%3 === 0 && i%5 === 0)
{
console.log("FizzBuzz");
}
else
{
console.log(i);
}
}


#4

I have made few changes in your code and it is working fine now.

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

for (y = 0; y < 20; y++){
if ((x[y] % 3 === 0) && (x[y] % 5 === 0)) {
console.log("FizzBuzz")}
else if (x[y] % 5 === 0 && x[y]%3 !== 0) {
console.log("Buzz")}
else if (x[y] % 3 === 0 && x[y]%5 !== 0) {
console.log("Fizz")}
else {
console.log(x[y])}

};


#5

thanks that worked great!Thank you all for the help and especially jayanti88k. It was driving me crazy but it is working now..

Thanks again,
Dr K


#6

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