Check porcentage of two values


#1

Hello friends,

How do i solve this? When the division of 3 and 5 is iqual 0 the code must print "FizzBuzz".

But it is not working. Help!

var gnumer = [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 < 20; i = i + 1 ){
gnumer.length;
if ((gnumer[i] % 3) === 0){
console.log("Fizz");
}else if ( (gnumer[i] % 5) === 0 ){
console.log("Buzz");
}else if( ((gnumer[i] % 3) === 0) && ((gnumer[i] % 5) === 0) ){
console.log ("FizzBuzz");
}else{
console.log(gnumer[i]);
}
}


#2

agree with me that when the if condition is true, else if and else won't run since the condition is already met?

we can walk through it with a number which is a fizzbuzz (15 for example):

if ((15 % 3) === 0)

this is true, so log fizz and move on to the next number

How could we overcome this problem? Maybe changing the order of the conditions?


#3

like stetim94 has mentioned, be careful on the order of the condition.

your conditions are like this:
1. if it is divisible by 3
2. if it is divisible by 5
3. if it is divisible by 3 AND divisible by 5
4. if none is true.

the problem will be, if a number is divisible by 3 and 5, it will surely be filtered by rule 1 and rule 2 first, and will never get filtered by rule 3 (and that means there will never be a FizzBuzz) I hope this is clear enough.


#4

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