CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2014
    Posts
    2

    Need some help understanding what this code does.

    Hello everyone, I'm extremely new to using JavaScript, and I have been working on a project for school. The last part I need to finish is to randomize a set of 24 divs. So it takes the divs I already have, and randomly swaps them around. I found this code online:

    $(function () {
    var parent = $("#shuffle");
    var divs = parent.children();
    while (divs.length) {
    parent.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);
    }

    But I am not 100% sure what it is doing, or how it is exactly doing it. My code works perfect using this, and my entire project is complete, but I don't want to use it and have no idea what it does or how it works.

    Can anyone explain it to me?

    So far what I think it does is take a variable, parent, and set it equal to my div element that has the ID of shuffle.
    Then it creates a divs variable, and creates an array of all of it's children (all of the divs inside of that div)
    Then there is a while loop that runs while there is still a variable inside of the divs array.

    Please correct me if anything is wrong, and then please explain that last line of code that performs the randomizing.

    Thanks everyone in advance,

    -Josh

  2. #2
    Join Date
    May 2014
    Posts
    2

    Re: Need some help understanding what this code does.

    Bah... one of those lines should say:
    Then it creates a divs array, which is filled with all of the children of the shuffle ID (all of the divs inside of the shuffle div)

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Need some help understanding what this code does.

    This is a java forum. Try a javascript forum for this question.
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured