I am shuffling all the elements in an array by changing the index at which it appears normally. It works well but the problem is that I want to keep the array state before I shuffle it but it is not working because when I save the array before I shuffle it the array's first call takes the same array state as the shuffled array. I want to know why?
See below illustration of the behavior...
$(document).ready( event=>{
let imager =[];
$("img").map( (n,e)=>{
imager.push({n,e})
})
console.log(imager) // saved version of imager before I shuffle :Why it takes the state of the shuffled array here
shuffle2(imager) // I shuffle the array
console.log(imager) // shuffled version
})
// these are images element in the dom inside html
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_001.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_002.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_003.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_004.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_005.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_006.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_007.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_008.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_009.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_010.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_011.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_012.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_013.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_014.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_015.jpg">
<img src = "https://raw.githubusercontent.com/thetalent/omapuzzle16/master/image_part_016.jpg">
// the shuffle function
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}