1

I am trying to loop through all the contacts on a phone and pick one contact randomly. I have written the code below (in JavaScript) and it is returning a single random letter.

function callme(){
    var options = new ContactFindOptions( );
    options.filter = ""; 
    options.multiple = true; 
    var filter = ["displayName"]; 

navigator.contacts.find(filter, successFunc, errFunc, options); // ...?

//alert("present");

function successFunc( matches ){
  for( var i=0; i<matches.length; i++){
    var myArray = matches[i].displayName;
    var random_contact = myArray[Math.round(Math.random() * (myArray.length - 1))];
  }

  alert(random_contact); //alerting random letter
}

function errFunc(){
    alert("oh no!");
}   


};
Ankur Aggarwal
  • 2,993
  • 5
  • 30
  • 56
MissElizabeth
  • 519
  • 1
  • 5
  • 11

1 Answers1

2

myArray contains a string (displayName), so yes, a random position in it is a single letter. Try seeking a random position in matches and return matches[random].displayName instead.

SVD
  • 4,743
  • 2
  • 26
  • 38