最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript if statement test for one or the other but not both - Stack Overflow

programmeradmin1浏览0评论

Hello fellow StackOverflowers. I'm have a brain fart right now, and I cannot seem to figure this out.

I have the following code

if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0))
    {
        return true;
}
else {
    return false;
}

Basically I need to test if the number is a multiple of 3 || 5 but not a multiple of both.

However when I enter any number I enter (whether it is multiple of 3 || 5 || both) the test always fails. I would have thought this was able to be performed in one statement.

This code though does work fine.

if (n % 3 === 0 || n % 5 === 0)
{
    if( n % 3 === 0 && n % 5 === 0)
    {
        return false;
    }
    else {
       return true;
    }
}
else {
    return false;
}

But I'm wondering what I am missing in the first test. I'd like all the test to be in one like, but like I said I'm having a brain fart and cannot figure out what I'm missing.

Hello fellow StackOverflowers. I'm have a brain fart right now, and I cannot seem to figure this out.

I have the following code

if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0))
    {
        return true;
}
else {
    return false;
}

Basically I need to test if the number is a multiple of 3 || 5 but not a multiple of both.

However when I enter any number I enter (whether it is multiple of 3 || 5 || both) the test always fails. I would have thought this was able to be performed in one statement.

This code though does work fine.

if (n % 3 === 0 || n % 5 === 0)
{
    if( n % 3 === 0 && n % 5 === 0)
    {
        return false;
    }
    else {
       return true;
    }
}
else {
    return false;
}

But I'm wondering what I am missing in the first test. I'd like all the test to be in one like, but like I said I'm having a brain fart and cannot figure out what I'm missing.

Share Improve this question asked Apr 4, 2012 at 14:32 ChrisChris 2595 silver badges12 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 9

You can use the XOR operator, alternatively

return  (n % 3 === 0 ^ n % 5 === 0);

If it is divisible by both 3 and 5, it'll be divisible by 15.

Please try the following condition

if ((n % 3 === 0 || n % 5 === 0) && ( n % 15 !== 0))

change

if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0))

to

if ((n % 3 === 0 || n % 5 === 0) && !(n % 3 === 0 && n % 5 === 0))

The first part of your logic is to determine if the number in question is a multiple of 3 or 5 whereas the second SHOULD be about wether only one of them is. So... I changed the second part to see if both match it and then I NOT'ed that.

It should be: if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 || n % 5 !== 0))

return (n % 3 === 0  && !(n % 5 === 0)) || (n % 5 === 0  && !(n % 3 === 0));

(untested)

Your second check is wrong:

if ((n % 3 === 0 || n % 5 === 0) &&**( n % 3 !== 0 && n % 5 !== 0)**)

Change it to:

(! (n%3 === 0 && n % 5 === 0 ) )

This is a short version of XOR implementation using conditional statement in javascript

if((n % 3 === 0)? (n % 5 !== 0) : (n % 5 === 0)) {
  ...
}

or you can also compare in this way, checking when the two conditions, when evaluated as boolean, return different values (one is true and other is false or vice-versa)

if( (n % 3 === 0) !==  (n % 5 === 0)) {
  ...
}

so this code can be written really short

发布评论

评论列表(0)

  1. 暂无评论