function canPartition(arr) {
let i=0;
while(i< arr.length){
let temp = arr;
let val = temp[i];
temp.splice(i,1)
//console.log(temp)
//console.log(val +":"+ temp.reduce((a,b)=> a*b))
if(val == temp.reduce((a,b)=> a*b)){
return true;
}
i++;
}
return false;
}
console.log(canPartition([-1, -20, 5, -1, -2, 2]))
What exactly happening here is the value of arr got changed when I am performing splice on temp i.e referenced to arr. How can I prevent this, I wants arr to be the same and when i value increased, it again get assign to temp.
Any help is appreciable thanks in advance