# Api for loop problem

**URL:** <https://forum.freecodecamp.org/t/api-for-loop-problem/152054>\
**Category:** General\
**Created:** [October 8, 2017, 1:11pm UTC](https://forum.freecodecamp.org/t/api-for-loop-problem/152054 "2017-10-08T13:11:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Folrunge](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/folrunge/32/48799_2.png) [@Folrunge](https://forum.freecodecamp.org/u/Folrunge)\
**Post date:** [October 8, 2017, 1:11pm UTC](https://forum.freecodecamp.org/t/api-for-loop-problem/152054/1 "2017-10-08T13:11:26Z")

</div>

Hi there!

Some time ago I was asked to make a very basic and simple cat-gif app where you push a button and an ajax call is executed resulting in appending random cat gif to a div. I made the call and used `for` loop to go through JSON data but it **always** results in appending the last element of the array (i.e in 15 objects array it appends number 15). Manually inserting array object’s number instead of `[i]` gives a positive result (not random though). So my feeling is that I did something wrong with my `for` loop.

```auto
$(document).ready(function(){
  $('button').on('click', function(){
// API's url 
    var url = 'https://api.giphy.com/v1/gifs/search?q=cat&api_key=c3839c06e9714ed688ffc1a6d5cb14a7&limit=15';
// ajax call
    $.ajax({
      url: url, method: 'GET'
    }).done(function(succ){
// Variable for my convenience. In JSON API's callback there is an array called "data" which stems into various objects
      var out = succ.data;
// for loop searching through the array and inserting the result into div with "foto" id
      for (var i=0; i<out.length; i++){
         $('#foto').attr('src', out[i].images.fixed_height.url); 
      }
    });
    return false;
  });
});

```

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![moT01](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mot01/32/417588_2.png) [@moT01](https://forum.freecodecamp.org/u/moT01)\
**Post date:** [October 8, 2017, 1:21pm UTC](https://forum.freecodecamp.org/t/api-for-loop-problem/152054/2 "2017-10-08T13:21:48Z")

</div>

iIf I’m understanding this right - instead of a for loop there, you should just generate a random number and use it to get the image from your data.

```auto
var randomNumber = get a random number in the range you want
out[randomNumber].images.fixed_height.url

```

something like that maybe?

---

<div class="post-metadata">

**Author:** ![Folrunge](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/folrunge/32/48799_2.png) [@Folrunge](https://forum.freecodecamp.org/u/Folrunge)\
**Post date:** [October 8, 2017, 1:35pm UTC](https://forum.freecodecamp.org/t/api-for-loop-problem/152054/3 "2017-10-08T13:35:34Z")

</div>

Oh, right! Didn’t thought about using `Math.random` for getting the results. It works fine now but have to tinker to out the repetition problem.Thanks!😉

---

<div class="post-metadata">

**Author:** ![Folrunge](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/folrunge/32/48799_2.png) [@Folrunge](https://forum.freecodecamp.org/u/Folrunge)\
**Post date:** [October 9, 2017, 10:00am UTC](https://forum.freecodecamp.org/t/api-for-loop-problem/152054/6 "2017-10-09T10:00:58Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:37pm UTC](https://forum.freecodecamp.org/t/api-for-loop-problem/152054/7 "2021-01-17T20:37:59Z")

</div>


