Recently I am practicing algorithm questions. There is one that calculates if a string is lexicographically smaller or bigger. I have to find digits, remove them one by one to compare with other string.
I.E -- 'ab23f' >> 'ab3f' >> 'ab2f'
function test(a1,a2){
let str1 = a1.split("");
let str2 = a2.split("");
for(let i = 0; i<str1.length;i++){
let clone = str1;
if(!isNaN(clone[i])){
let index = str1.indexOf(str1[i]);
clone.splice(index,1);
}
}
}
test('ab12c','1zz456');
The problem is; Should not it be reassigning the clone variable to str1 in every loop? I have to keep the original array safe so that I can remove the digits one by one. so I found this solution to make a clone and in start of every loop, it should reassign itself same as original array but clone variable does not refresh/reassign.
I have tried to take the clone variable out of the block but nothing changes.