Home > Back-end >  google apps script - functi "cari" produces inappropriate results
google apps script - functi "cari" produces inappropriate results

Time:07-14

error on data that does not match if the data entered does not match the data sought then the code will repeat up to 12 times, picture => 1

If the data entered is correct, the result is correct, picture => 2

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 = pola[1] ' adalah ' rs[i][1];
        sendMessage(cid,txt);
      } else{
        txt = 'Data ' pola[1] ' tidak ditemukan atau perintah command tidak sesuai...';
        sendMessage(cid,txt);
      }
    }
  }
}

CodePudding user response:

Move the else block {} outside the loop:

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 = pola[1] ' adalah ' rs[i][1];
        sendMessage(cid,txt);
      } 
    } //end of loop
    txt = 'Data ' pola[1] ' tidak ditemukan atau perintah command tidak sesuai...';
    sendMessage(cid,txt);          
  }
}
  • Related