Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda