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 8347045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:13:42+00:00 2026-06-09T07:13:42+00:00

Sorry if this question is dumb, I don’t think it is because I can’t

  • 0

Sorry if this question is dumb, I don’t think it is because I can’t find a straight answer. I’m trying to implement the standard Fisher-Yates shuffle, but on a jQuery-wrapped array of DOM elements. I.e. the usual ‘swap’ algorithm.

The idea was to implement shuffling a deck of cards.

My initial simple (but hacky) solution:

1) assign an index attribute to each DOM element, and at the same time create a list of indices.

indices = [];
deck.each(function(i){
    $(this).attr('index',i);
    indices[i] = i;        
});

2) shuffle the indices array,

for(i=0;i<indices.length;i++){
    temp = indices[i]; 
    j = Math.floor((Math.random()*i));
    indices[i]=indices[j];
    indices[j]=temp; 
}

and iterate through them, using something like, I dunno, say:

for(i=0;i<indices.length;i++){
     randomCard = $('[index='+indices[i]+']');
     //do amazing game-like things
}

BUT…I hate this solution, it feels extremely hacky. I’d much rather manipulate the wrapped set.


SO…

Once again here’s the pseudo code for the Fisher-Yates shuffle.

deck.each(function(i){ 
    temp = deck[i]; //except that I want deck[i] to be a **wrapped object**

    j = Math.floor((Math.random()*i));

    deck[i]=deck[j]; //and deck[j] should find a wrapped object in the 'elements' set with index j

    deck[j]=temp; //and this should perform the swap.

    alert("I can haz swapburgers!!!");  //yay.
});

The part I don’t understand how to do is to set the value of an element in a jQuery wrapped object by its index. Should I be passing the indices and the parent element to a swap function? Something like

swap($(this).parent(),i,j);

If anyone can help me sort this out I’d appreciate it. I hope all of this is clear. Please let me know if it’s confusing and I’ll try to clarify.

  • 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-06-09T07:13:44+00:00Added an answer on June 9, 2026 at 7:13 am

    It looks like you can just treat the jquery selection like a normal array, as far as swapping goes.

    function swap(obj, index1, index2) {
        var temp = obj[index1];
        obj[index1] = obj[index2];
        obj[index2] = temp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is a really dumb question - I can't find the answer
I'm sorry if this will sound like a dumb question but I just can't
Sorry if this is a dumb question ... I'm just trying to learn the
Sorry if this is a dumb question. I don't ever have to write anything
sorry for asking this dumb question i will try to explain as good as
Sorry if this is a dumb question but im creating a filter class that
Sorry if this is a dumb question. I'm using .each to loop through a
Sorry if this is a dumb question, but it is possible to store, and
I am just starting out C++, so sorry if this is a dumb question.
Sorry if my question seems dumb. I've started using Eclipse Ganymede 3.4 this week

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.