CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2020-11-17
by baymax

Original Post

Original - Posted on 2010-03-15
by ChristopheD



            
Present in both answers; Present only in the new answer; Present only in the old answer;

You should build a function to shuffle items randomly. Try below.
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const list = [ {id: 0, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 1, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 2, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 3, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 4, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 5, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 6, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 7, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 8, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false}, {id: 9, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false} ]; const shuffle = array => { let currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;
// And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; }
return array; };
console.log(shuffle(list))
<!-- end snippet -->

The de-facto unbiased shuffle algorithm is the Fisher-Yates (aka Knuth) Shuffle.
See https://github.com/coolaj86/knuth-shuffle
You can see a [great visualization here][0] (and the original post [linked to this][1])
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle... while (0 !== currentIndex) {
// Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;
// And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; }
return array; }
// Used like so var arr = [2, 11, 37, 42]; shuffle(arr); console.log(arr);
<!-- end snippet -->
Some more info [about the algorithm][2] used.
[0]: http://bost.ocks.org/mike/shuffle/ [1]: http://sedition.com/perl/javascript-fy.html [2]: http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

        
Present in both answers; Present only in the new answer; Present only in the old answer;