Trying to see if my array is divisbile by 3 and 5, but wont work. Suggestions?


#1

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

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

#2

Hi @griffithpoindexter

You have to put

else if(numbers[i] % 5 === 0 && numbers[i] % 3 ===0 )

in first condition, because if he finds a value divisibile by 3 or 5 before he is going to go out and doesn't arrive to this condition

Your code should looks like this

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

for(var i = 0; i < numbers.length; i++){

if(numbers[i] % 5 === 0 && numbers[i] % 3 === 0){
        console.log("Fizz Buzz");  
    }else if(numbers[i] % 5 === 0){
        console.log("Buzz"); 
    }else if(numbers[i] % 3 === 0){
        console.log("Fizz"); 
    }else{
        console.log(numbers[i]);
    };
};
};

#3

also u dont need to make an array
u can use for loop as this,,

for(var i = 1; i < 21; i++){
// if else statement here
};


#4

Yes of course, I just wanted to take your example. It's good what you seen that :smile: