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

216
Vistas
Random Infinite-loop in Javascript

I'm having an issue with this code I'm writing for a challenge.
The code I wrote goes in an infinite loop and I can't understand what's wrong.

var wiggleMaxLength = function(nums) {
  let wiggleSteps = 1;
  let actualDiff, pastDiff, i;
  for (i = 1; i < nums.length; i++) {
    actualDiff = (nums[i] - nums[i - 1])
    if (nums[i] = 0) {
      nums.splice(i, 1);
      i--;
      continue;
    } else if ((pastDiff > 0 && actualDiff < 0)  || (pastDiff < 0 && actualDiff > 0) || (i = 1)) {
      pastDiff = actualDiff;
      wiggleSteps++;
    } else {
      break;
    }
  }
  return wiggleSteps;
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There is a typo on line 6 (= instead of == or ===)

if (nums[i] = 0)

I assume that you didn't see it.

This assigns the value 0 to nums[i], instead of comparing nums[i] to 0.

Therefore, you should change the if into

if (nums[i] == 0)

or

if (nums[i] === 0)

Note that there is also an additional typo on line 9 in the 3rd condition i = 1 instead of i == 1 or i === 1, which is the reason that causes the infinite loop (as the counter 'i' is always re-assigned to 1). The program always goes on the else if branch.

about 4 years ago · Juan Pablo Isaza Denunciar
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