Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you have a few issues there: </p> <ul> <li><p>variables in the global window scope </p></li> <li><p>an array declared with the <code>new</code> keyword instead of a literal </p></li> <li><p>trying to use variables before declaring them</p></li> <li><p>jQuery.inArray being incorrectly used (<a href="http://api.jquery.com/jQuery.inArray/" rel="nofollow">inArray returns a number</a>, not <code>true</code> or <code>false</code>)</p></li> <li><p>inefficient code with a <code>while</code> loop which theoretically could lead to an infinite loop</p></li> </ul> <p>now, the second combined with the third is the main issue here, as the first time you test for <code>x</code> in the array it is <code>undefined</code> (you are only defining it inside the <code>if</code> with a <code>var</code> statement, so the x is at first undefined) and thus it matches the first element in the array (which is <code>undefined</code> as you declared <code>r</code> with <code>new Array(6)</code>) and the inArray function returns 1, which leads to an infinite loop.</p> <p>There are several things you could do to patch that code, but I think a complete rewrite with a different approach might be better and requires no jQuery at all.</p> <p>This modified version of your code should work fine:</p> <pre><code>var Show = 6, // Number of logos to show TotalLogos = 21, // Total number of logos to choose from FirstPart = '&lt;img src="/wp-content/client-logos/logo', LastPart = '.jpg" height="60" width="120" /&gt;', array = [], // array with all avaiilable numbers rnd, value, i, logosElement = document.getElementById('client-logos'); for (i = 0; i &lt; TotalLogos; i++) { // arrays are zero based, for 21 elements you want to go from index 0 to 20. array[i] = i + 1; } for (i = 0; i &lt; Show; i++) { // pick numbers rnd = Math.floor(Math.random() * array.length); value = array.splice(rnd,1)[0]; // remove the selected number from the array and get it in another variable logosElement.innerHTML += (FirstPart + value + LastPart); } </code></pre> <p><br></p> <p>To explain a little what I did here:</p> <ul> <li><p>initialize an array with all the possible values you have (numbers 1 to 21)</p></li> <li><p>run a loop only as many times as numbers you want to pick.</p></li> <li><p>generate a random number from 0 to the maximum index available in your numbers array</p></li> <li><p>remove the number at that index from the array using splice, and then use it to create the string for the innerHTML call (<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice" rel="nofollow">splice returns the elements removed from the array as another new array</a>).</p></li> <li><p>additionally, the <code>logosElement</code> variable is cached at the beginning so you only do a single DOM lookup for the element.</p></li> </ul> <p>There are more ways that code can be rewritten and even cleaned up, but I figured this would be the cleanest way to help you with your issue (and it's cross-browser safe! no need to add jQuery unless you need it for another functionality)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload