How do I take the output from a shuffle function and moves around DOM elements?

Sparrky

So I have this HTML:

 <ul class="deck">
        <li class="card">
            <i class="fa fa-diamond"></i>
        </li>
        .....(16 total list items here)
    </ul>

And this javascript:

const arrayOfCards = $('.card');

function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

while (currentIndex !== 0) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
}
return array;
}
shuffle(arrayOfCards);

That function takes the cards (it's a card memory game) and shuffles them, but it doesn't move around the cards...I need to somehow move the cards around, I don't know how to do it. Should I, instead of having that HTML, create the cards (list items) dynamically from jQuery or what? I'm really confused here.

charlietfl

You need to append the sorted array to the dom. What you are currently doing is only sorting an array that happens to contain elements but that sorted array doesn't automatically change the dom

Here's a very simple shuffle approach.

var $deck = $('.deck'),
  $cards = $deck.children();

$('button').click(function() {
  // sort the elements
  $cards.sort(function() {return Math.random() - .5; });
  // update dom with new sort
  $deck.append($cards);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="deck">
  <li class="card">1</li>
  <li class="card">2</li>
  <li class="card">3</li>
  <li class="card">4</li>
  <li class="card">5</li>
  <li class="card">6</li>
  <li class="card">7</li>
  <li class="card">8</li>
  <li class="card">9</li>
  <li class="card">10</li>
</ul>

<button>Shuffle</button>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i take the input quote from a user and shuffle it then put every letter separate in a table in JavaScript

How do I take the mean of this output

How do I randomly shuffle a subset of the elements of an existing matrix in Matlab?

Lua: How do I shuffle certain elements in an Array?

How do I pass the app around function?

How do I stop elements from moving around on a web page after the width of the page is sufficiently low?

How do I navigate dom elements in puppeteer?

How do I control DOM elements with FreshJs

How to take specific elements from the output of command and then display in a table format?

How do I output from a (doubly) recursive function?

How do I get a multicharacter output from input() function in Python

How do I create concave shaped borders around <div> elements?

how do I move around java gui elements?

How do I make an SVG take no space in the DOM?

How do I create a Vec from a range and shuffle it?

How do I shuffle a VecDeque?

How do I shuffle a list?

How do I write methods that take the output of the .col() method as input?

How do I take out whitespaces in bs4 output

How do you shuffle elements in a Map

How do I get around aggregate function error?

How do I detect a finger moves into or moves out of a widget

How can I take as input, the output from another c file?

How can I design an r function that selects specific elements from a list of lists, and returns a dataframe as an output

How do I take input from the the sub class here? When I run this code it uses the values from superclass in Move() Function

How do I take input from the user of their full name, and then output their first name and last name seperately? (C++)

How do i store output of the function into an array

How do I export the output of an async function?

How do I prevent tmux from wrapping around when searching?