-2

I'm working on a drag and drop card game. I'm able to use sort and use Math.Random to shuffle:

answers.sort( function() { return Math.random() - .5 } );

It works but after the cards are placed in the correct subcontainer the wrong year appears below as if it's shuffling.

Is it possible to shuffle an array by the image but still be able to sort by value(order)?

  var answers = [
    {
      "image":'https://www.floridamemory.com/onlineclassroom/game/cards/1822.png',
      "order":'1'
    },{
      "image":'https://www.floridamemory.com/onlineclassroom/game/cards/1839.png',
      "order":'2'
    },
    {
      "image":'https://www.floridamemory.com/onlineclassroom/game/cards/1912.png',
      "order":'3'
    }
  ];
  • 3
    What do you mean shuffle by the image? – gabriel.hayes Oct 24 '19 at 19:39
  • 4
    What do you mean by "shuffle an array by the image"? – VLAZ Oct 24 '19 at 19:39
  • It seems like you don't need JSON objects, but just a one-dimensional array of image URLs. And then your "order" is just the index of the image URLs. As such, you could just shuffle the array of strings like any other array. – Marc Oct 24 '19 at 19:44
  • I'm confused about the point to this. You want to shuffle them but then also have them be ordered by the order? Either way one or the other is going to overrule the other. – Matt Croak Oct 24 '19 at 19:48
  • Sidenote: [Don't use `.sort` and `Math.random` to shuffle arrays](https://stackoverflow.com/questions/962802/is-it-correct-to-use-javascript-array-sort-method-for-shuffling). It is not as random as you would expect. – VLAZ Oct 24 '19 at 20:01
  • I just edited my post – Shanice Morgan Oct 24 '19 at 20:01
  • Can you please share your HTML code? Because shuffling is normally fine - if that puts the elements in the wrong HTML containers, then *that* is the problem, not the shuffling itself. – VLAZ Oct 24 '19 at 20:02
  • @VLAZ yeah I noticed that Math.random isn't the best but I thought that it would be okay with something small I guess – Shanice Morgan Oct 24 '19 at 20:03
  • @VLAZ https://codepen.io/shanice92/pen/pooPNOw – Shanice Morgan Oct 24 '19 at 20:04

1 Answers1

0

Solved it by following Order two Arrays based on the same random sorting system

Just added an extra array to help the shuffle