sendMessage executing twice even after if condition is fulfilled

Viewed 46

I have a problem with the "cari" function, the data is correct but there is a

        txt = 'Material '+pola[1]+' tidak ditemukan atau perintah command tidak sesuai';
        sendMessage(cid,txt);
still appeared which is, it's no need to appear when the data is correct.
enter image description here enter image description here

function cari(cid,cmd){
  var txt = '';
  var pola = cmd.match(/cari#(.+)/);
  if (pola[1]!=''){
    var rs = bacadata();
    for (var i=0;i<rs.length;i++) {
      if (rs[i][0] == pola[1]){
        txt = 'Material '+pola[1]+' adalah '+rs[i][1];
        sendMessage(cid,txt);
      }
    }
    txt = 'Material '+pola[1]+' tidak ditemukan atau perintah command tidak sesuai';
    sendMessage(cid,txt);
  }
}

1 Answers

Try sendMessage() once after the loop:

function cari(cid,cmd){
  var pola = cmd.match(/cari#(.+)/);
  var txt = 'Data '+pola[1]+' tidak ditemukan atau perintah command tidak sesuai...';
  if (pola[1]!=''){
    var rs = bacadata();
    for (var i=0;i<rs.length;i++) {
      if (rs[i][0] == pola[1]){
        txt = pola[1]+' adalah '+rs[i][1];
        break;
      } 
    } //end of loop 
    sendMessage(cid,txt);          
  }
}
Related