Need a help with simple js

Hey so I’m trying to make a code that will do

  1. If n is a multiple of 4 and 7, it will print ‘WhiteBlack’
  2. If n is a multiple of only 4 it will print ‘White’
  3. If n is a multiple of only 7 it will print 'Black
  4. If n is not a multiple of 4 nor 7 it will print n
    so this is my code so far
function whiteBlack(n) {
        for(let i = 0; i < n; i++){
        if(i = n % 4){
            console.log('White');
        }else if(i = n % 7){
            console.log('Black');
        }else if(i = n % 4){
            if(i = n % 7){
            console.log('WhiteBlack');}
        }else {
           console.log(i);
        }
    }
}

this will print 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I tried other possible solution that I know which is

function whiteBlack(n) {
        let final = [];
    for(let i = 0; i < n; i++){
        if(i = n % 4){
            final = final.push('White\n');
        }else if(i = n % 7){
            final = final.push('Black\n');
        }else if(i = n % 4){
            if(i = n % 7){
            final = final.push('WhiteBlack\n');}
        }else if(i != n % 4){
            if(i != n % 7){
            final = final.push(i);}
        }
    }
 return final;
}

but this doesn’t print anything.
Do you know what’s wrong with my code?

You keep using = in your if statements. That is the assignment operator, not for comparison.

1 Like

Oh yeah right,
I wouldn’t realise it if you didn’t point it out.
Thank you!

1 Like