Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature currently requires accessing the site using the built-in Safari browser.
Durchgemischt bekommst Du es schon bei dem Code, den Du hast, nur dass die originale Version verloren geht. Wenn Du diese behalten möchtest, musst Du das Array klonen, so wie es in dem Link in #6 beschrieben wird.
function erstellKombination() {
var buch1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
var buch2 = ["H", "I", "j", "k", "l", "m", "n", "o", "p", "q"];
var zahl = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function cloneArray(arr) {
let newArray = [];
for (let i = 0; i < arr.length; i++) {
newArray[i] = arr[i];
}
return newArray;
}
var zw1 = cloneArray(buch1);
shuffleArray(zw1);
var zw2 = cloneArray(buch2);
shuffleArray(zw2);
var zw3 = cloneArray(zahl);
shuffleArray(zw3);
console.log("buch1 durchgemischt:" + zw1);
console.log("buch2 durchgemischt:" + zw2);
console.log("zahl durchgemischt:" + zw3);
var gesuchtBasis = [];
for (let i = 0; i < 10; i++) {
gesuchtBasis[i] = zw1[i] + zw2[i] + zw3[i];
}
gesuchtNeu = cloneArray(gesuchtBasis);
shuffleArray(gesuchtNeu);
console.log("gesuchtBasis in erstellkombination " + gesuchtBasis);
console.log("gesucht neu " + gesuchtNeu);
}
erstellKombination();
Man hätte es noch etwas eleganter machen können, indem man die shuffle-Funktion als Prototyp für Array definieren würde.
Siehe auch Why is extending native objects a bad practice?.Das Verändern eines built-in Prototypes war noch nie eine gute Idee.