0

I'm getting my response from my GET requests to this API that I'm working with which is great. I just need the responses to be random every time I click my button, but I'm having trouble with that. I have a math.random function below in my code, but I don't have it active right now because I'm not sure what to do with it. I've tried some different approaches but nothing seems to work.

$(document).ready(function(){


    //When you click the submit button it fires off the getRequest.
    $(function(){
      $('#search-term').submit(function(e){
         e.preventDefault();
            myFunction();
            //clearText();
      });
    });



    // This is the get request. I want it to have a random function that randomizes the callback data. Once you get the randomizes data, it shows in the console window. I want this function display three random results based on the myFunction() below
    function getRequest(){
      $.ajax({
        url:'https://api.foursquare.com/v2/venues/search? ll=40.7,-74&client_id={my client id]&client_secret=[my secret]',
        type: 'GET',
        dataType:'json',
        success: function(data){
          var venues = data['response']['venues'];

          //This is my random function which I do not know what to do with
          var random = [Math.floor(Math.random() * venues.length)];

           $.each(venues, function(key,index){
             console.log(index.name);
             $('#search-results').append(index.name + '<br>');
           })
         }
       })
     };



    //This is the function that calls getRequest function three times then stops.
    function myFunction(){
        var myVar = setInterval(function(){getRequest();}, 500);
        //clearTimeout(myVar);
        setTimeout(function( ) { clearInterval( myVar); }, 1600);
    }


    //This function clears the text that exists in the form so that the new random text can populate it.
    function clearText(){
      $("#search-results").text("");
    };
});
user5664615
  • 400
  • 1
  • 10
  • 17
thesubhuman
  • 105
  • 1
  • 9
  • 3
    What exactly should be randomized? – Federico klez Culloca Feb 02 '18 at 15:20
  • Id like console.log(index.name) to be random. – thesubhuman Feb 02 '18 at 15:34
  • 2
    you want to print a random value from `venues` array? – messerbill Feb 02 '18 at 15:35
  • 1
    Like… shuffle the venues in a random order? Or pick just one random venue? – helb Feb 02 '18 at 15:35
  • 1
    Possible duplicate of [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) – Liam Feb 02 '18 at 15:36
  • If you want to print a random venue from the list, just use `console.log(venues[random].name);`. If you want to print them all in a random order, check out the answers to this question https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – user5664615 Feb 02 '18 at 15:43
  • I Id like the venues to be shuffled randomly. So every time I click my button, the venues are shuffled. – thesubhuman Feb 02 '18 at 15:55
  • 1
    Possible duplicate of [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – user5664615 Feb 02 '18 at 16:12

0 Answers0