I don't want to modify puzzle1, that is why I clone it to puzzle0. However, when I console.log puzzle1 I got the same value of value0. Is there any way to keep the value of puzzle1. I am new to JavaScript so this feature is kind of weird.
function removeOutOf81(puzzle1, numToRemove) {//modify grid by removing randomly numToRemove tiles
let arr = [];
console.log(puzzle1);
let puzzle0 = puzzle1; //clone not modify
for(let i=0;i<81;i++) arr[i] = i;
for (let i=81;i>numToRemove;i--){
//return a number from 0 to i-1
let rmIndex = getRandomInt(i);
puzzle0.splice(arr[rmIndex],1,'-');
arr.splice(rmIndex, 1);
}
console.log(puzzle0);
console.log(puzzle1);
return puzzle0;
}
What it prints:
//puzzle1 before modifying puzzle0
[ 4, 8, 6, '-', '-', '-', '-', '-', '-', 9, 5, 3, '-', '-', '-', '-', '-', '-', 1, 2, 7, '-', '-', '-', '-', '-', '-', '-', '-', '-', 6, 4, 2, '-', '-', '-', '-', '-', '-', 3, 9, 8, '-', '-', '-', '-', '-', '-', 1, 7, 5, '-', '-', '-', '-', '-', '-', '-', '-', '-', 4, 3, 5, '-', '-', '-', '-', '-', '-', 7, 8, 9, '-', '-', '-', '-', '-', '-', 2, 6, 1 ]
//puzzle0 after being modified
[ '-', '-', 6, '-', '-', '-', '-', '-', '-', 9, 5, 3, '-', '-', '-', '-', '-', '-', 1, 2, 7, '-', '-', '-', '-', '-', '-', '-', '-', '-', 6, 4, 2, '-', '-', '-', '-', '-', '-', 3, 9, 8, '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', 3, 5, '-', '-', '-', '-', '-', '-', '-', '-', 9, '-', '-', '-', '-', '-', '-', 2, 6, 1 ]
//puzzle1 gets modified too [ '-', '-', 6, '-', '-', '-', '-', '-', '-', 9, 5, 3, '-', '-', '-', '-', '-', '-', 1, 2, 7, '-', '-', '-', '-', '-', '-', '-', '-', '-', 6, 4, 2, '-', '-', '-', '-', '-', '-', 3, 9, 8, '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', 3, 5, '-', '-', '-', '-', '-', '-', '-', '-', 9, '-', '-', '-', '-', '-', '-', 2, 6, 1 ]