Reputation: 311
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
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
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