user1729696
user1729696

Reputation: 311

Is there a logical difference between these two if blocks?

I am fairly certain both methods should accomplish the same thing. For some reason the first one will never enter the inner most block. The second method will return false if the condition matches. What changes when you put the incrementer in the if condition?

if (item.Reject)
{
    rejectCount++

    if (rejectCount >= maxRejects)
    {
        return false;
    }
}
else rejectCount = 0;

Second method:

if (item.Reject)
{
    if (++rejectCount >= maxRejects)
    {
        return false;
    }
}
else rejectCount = 0;

Upvotes: 0

Views: 47

Answers (2)

durbnpoisn
durbnpoisn

Reputation: 4669

According to a Jsfiddle I just wrote to test it, they do the exact same thing. I simplified it to test the conditions. But it serves the purpose

var rejectCount = 0;
var maxRejects = 2;

function incr1() {
    rejectCount++;
    if (rejectCount >= maxRejects) {
      //  return false;
    }

    alert(rejectCount);
}

function incr2() {
    if (++rejectCount >= maxRejects) {
      //  return false;
    }
    alert(rejectCount);
}

http://jsfiddle.net/durbnpoisn/j23uhfr0/

Upvotes: 1

Jatinder Kumar
Jatinder Kumar

Reputation: 513

Can you please provide value you are using for maxRejects?

Both methods are same, there is no reason for the first example Not to go in inner most block.

Upvotes: 0

Related Questions