JavaScript proj to print out numbers divisible by 3 and 5

Hi fellow students, I am trying to solve a question I found on a book which is from a range o numbers, print those divisible by 3, them print those only divisible by 5, them print those divisible by both 3 and 5. I have the following code by its not completely right…

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


for(let i = 0; i < numbers.length; i++){
  if(numbers[i] % 3 === 0){
    console.log(numbers[i] + ' is  div by 3');
  } else if(numbers[i] % 5 === 0){
    console.log(numbers[i]  + ' is  div by 3')
  } else if(numbers[i] % 3 === 0 && numbers[i] % 5 === 0){
    console.log(numbers[i] + ' are both div by 3 and 5' );
  } else {
    console.log("no fizz or buzz");
  }
}

15 (divisible by both 3 and 5) should log:

15 are both div by 3 and 5

yet the output you get is:

15 is div by 3

this is because if(numbers[i] % 3 === 0) is true for 15, so this condition evaluates to true, and the others are skipped

maybe you should change something about the order of your conditions?

You are checking divisibility by 5 and yet are printing ‘is div by 3’. That’s why you’re getting said output

Hi
sorry I have corrected that mistake already. here is my code:

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


for(let i = 0; i < numbers.length; i++){
  if(numbers[i] % 3 === 0){
    console.log(numbers[i] + ' is  div by 3');
  } else if(numbers[i] % 5 === 0){
    console.log(numbers[i]  + ' is  div by 5');
  } else if(numbers[i] % 3 === 0 && numbers[i] % 5 === 0){
    console.log(numbers[i] + ' are both div by 3 and 5' );
  } else {
    console.log(numbers[i] + " no fizz or buzz");
  }
}

but my second else if is not working, which is when a number is both div by 3 and by 5 at same time

Change the order of statements. Check for divisibility by 3 and 5 first, then individual tests by 3 and 5.

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


for(let i = 0; i < numbers.length; i++){
  if(numbers[i] % 3 === 0 && numbers[i] % 5 === 0){
    console.log(numbers[i] + ' is  div by both 3 and 5');
  } else if(numbers[i] % 5 === 0){
    console.log(numbers[i]  + ' is  div by 5');
  } else if(numbers[i] % 3 === 0){
    console.log(numbers[i] + ' is div by 3' );
  } else {
    console.log(numbers[i] + " no fizz or buzz");
  }
}

else if only evaluates when previous if/else ifs are false. Given a number that is divisible by both 3 and 5 is also divisible by either, the order of your conditions/clauses is very important

@tapputi mentioned changing the order, but does not explain why

Hi I got it right now:

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


for( let i = 0; i < numbers.length; i++){
  if(numbers[i] % 3 === 0 && numbers[i] % 5 === 0){
    console.log(numbers[i] + ' are both div by 3 and 5')
  } else if (numbers[i] % 3 === 0){
    console.log(numbers[i] + ' is divisible by 3')
  } else if (numbers[i] % 5 === 0){
    console.log(numbers[i] + ' is divisible by 5')
  } else {
    console.log('no fizz and buzz')
  }
} 

thank you,

However, I do not understand why you have a array of numbers, you could simply also use the loop?

for (let i = 1; i <= 20; i++){
   if (i % 3 === 0 && i % 5 === 0)
}

You are right, but I am still learning, I was just trying to solve the problem presented in the book, thank you for the tip