Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Why an array does not reassign in every loop?

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!