Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4040838
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:48:13+00:00 2026-05-20T12:48:13+00:00

This is a Javascript / jQuery question: I’m trying to generate six unique random

  • 0

This is a Javascript / jQuery question:

I’m trying to generate six unique random numbers between 1 and 21 (no duplicates), using the jQuery.inArray method. Those six numbers will then be used to select six .jpg files from a group named logo1.jpg through logo21.jpg.

Can anyone tell me what I’m doing wrong here?

<div id="client-logos"></div>

<script src="http://code.jquery.com/jquery-1.5.js"></script>

<script type="text/javascript">

Show = 6; // Number of logos to show
TotalLogos = 21; // Total number of logos to choose from
FirstPart = '<img src="/wp-content/client-logos/logo';
LastPart = '.jpg" height="60" width="120" />';
r = new Array(Show); // Random number array

var t=0;
for (t=0;t<Show;t++)
   {
      while ( jQuery.inArray(x,r)
         {
            var x = Math.ceil(Math.random() * TotalLogos);
         });
      r[t] = x;
      var content = document.getElementById('client-logos').innerHTML;
      document.getElementById('client-logos').innerHTML = content + FirstPart + r[t] + LastPart;
   }

</script>

Thanks in advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:48:14+00:00Added an answer on May 20, 2026 at 12:48 pm

    you have a few issues there:

    • variables in the global window scope

    • an array declared with the new keyword instead of a literal

    • trying to use variables before declaring them

    • jQuery.inArray being incorrectly used (inArray returns a number, not true or false)

    • inefficient code with a while loop which theoretically could lead to an infinite loop

    now, the second combined with the third is the main issue here, as the first time you test for x in the array it is undefined (you are only defining it inside the if with a var statement, so the x is at first undefined) and thus it matches the first element in the array (which is undefined as you declared r with new Array(6)) and the inArray function returns 1, which leads to an infinite loop.

    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.

    This modified version of your code should work fine:

    var Show = 6, // Number of logos to show
        TotalLogos = 21, // Total number of logos to choose from
        FirstPart = '<img src="/wp-content/client-logos/logo',
        LastPart = '.jpg" height="60" width="120" />',
        array = [], // array with all avaiilable numbers
        rnd, value, i,
        logosElement = document.getElementById('client-logos');
    
    for (i = 0; i < 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 < 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);
    }
    

    To explain a little what I did here:

    • initialize an array with all the possible values you have (numbers 1 to 21)

    • run a loop only as many times as numbers you want to pick.

    • generate a random number from 0 to the maximum index available in your numbers array

    • remove the number at that index from the array using splice, and then use it to create the string for the innerHTML call (splice returns the elements removed from the array as another new array).

    • additionally, the logosElement variable is cached at the beginning so you only do a single DOM lookup for the element.

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I am trying to do is very similar to this question: Using jQuery
This may be a general javascript or jQuery question- I'm using backbone.js and I'd
I'm really new to JavaScript and jQuery. My question is this. I have two
How can I do this in Javascript/jQuery from PHP? I am trying to populate
I'm trying to find the jQuery equivalent of this JavaScript method call: document.addEventListener('click', select_element,
I am using this jquery javascript function to show status message, function topBar(message) {
This is partly a jquery question, but most javascript. I just don't know javascript
I am a beginner when it comes to javascript/jquery ..so if this question sounds
Javascript and jQuery (Fancybox) question I'm using the Javascript function below for Twitter sharing
This might be a stupid question but is it possible to use javascript/jQuery in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.