user2470075
user2470075

Reputation: 169

Need an explanation on simple code

$('#ID').on('click', function() {
    if(!CommonUtil.compareDateById('startDt','endDt',false, false, true)) {
        return false;           
    }
    var cnt = 0;

    if(!CommonUtil.isNullOrEmptyById('startDt'))     { cnt++; }

if(cnt == 0) {
        CommonUtil.setFocusById('srchWord','<spring:message code="confirm.input" arguments="XXXX"/>');
        return false;

So if I click on #ID, following logic occurs. And my question is what does var cnt = 0;

if(!CommonUtil.isNullOrEmptyById('startDt')) {
    cnt++;
}

mean?

The function of isNullOrEmptyById is following:

isNullOrEmptyById: function(id) {
    var value = this.getTrimValueById(id);

    return this.isNullOrEmpty(value);
},

But what does

cnt++;

do in here??

Upvotes: -1

Views: 86

Answers (2)

David
David

Reputation: 218837

This is just an if conditional block:

if(!CommonUtil.isNullOrEmptyById('startDt')) {
  cnt++;
}

So if CommonUtil.isNullOrEmptyById('startDt') resolves to false, then the condition resolves to true and the code in the block is executed:

cnt++;

The ++ operator increments the value. So whatever numeric value is in cnt will be incremented by 1.

In the overall context of the code, it seems to be treating cnt as more of a boolean than an integer, though. Unless there's more code outside of this example, this can be simplified by using this condition for the last conditional block instead of using cnt and then checking its value.

Upvotes: 1

johnsoe
johnsoe

Reputation: 674

It is actually unnecessary. Since the cnt is only incremented once it's value is either 0 or 1. Instead you could just get rid of all that and use isNullOrEmptyById function.

if(!CommonUtil.isNullOrEmptyById('startDt')){
   CommonUtil.setFocusById('srchWord','<spring:message code="confirm.input" arguments="XXXX"/>');
   return false;  
}

Upvotes: 1

Related Questions