Why is this not working?


#1

<script>

function fizzBuzz()

{
    for(i = 1;i < 21;i++)
    {
        if(i % 3 === 0 || i % 5 === 0)  
        {
            if(i % 3 !== 0 && i % 5 !== 0)
            {
                console.log(i)  
            }
            if(i % 3 === 0 && i % 5 === 0)
            {
                console.log("FizzBuzz")  
            }
            if(i % 3 === 0 && i % 5 !== 0)
            {
                console.log("Fizz")  
            }
            if(i % 3 !== 0 && i % 5 === 0)
            {
                console.log("Buzz")  
            }

            
    } 
}
}



fizzBuzz()

.</script>

Try to execute this, it doesnt give me the results that I desire.


#2

When this is true

this will never be,

You should also know that the SCT is not expecting a function, so will never call it. The code needs to be in global scope, not inside a function.

There is a much simpler way to approach this problem that uses way less logic, and only one logical expression (or none).

if (i % 3 === 0 && i % 5 === 0) {    // or, if (i % 15 === 0) {
    // FizzBuzz
}
else if (i % 3 === 0) {
    // Fizz
}
else if (i % 5 === 0) {
    // Buzz
}
else {
    console.log(i);
}

#3

Thanks for the help I'll take that in mind