Home > OS >  How to print every number that is divisible by either 3 or 5, but not both?
How to print every number that is divisible by either 3 or 5, but not both?

Time:05-10

I have a function threeOrFive(max). It should loop every number that is divisible by either 3 or 5 but not both. So it won't loop 15 since it's divisible by both numbers but will print out "3, 5, 6 , 9, 10, 12, 18".

The code I've tried is

function threeOrFive(max){
    for(let i = 0; i<max; i =3, i =5){
        console.log(i);
    }
}

and

function threeOrFive(max){
    for(let i = 0; i<max; i =3){
        if(i =3 % max === 0 && i =5 % max === 0){
            return false;
        }
        console.log(i);
    }
}

CodePudding user response:

You can use this function. This will return a array of all numbers that divisible by 3 or 5 but not by both.

const threeOrFive = max => {
    const store = [];

    for(let i = 0; i < max; i  ){
        if (i%3==0 && i%5==0) continue;
        else if (i%3==0) store.push(i);
        else if (i%5==0) store.push(i);
    }

    return store;
}
  • Related