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

107
Vistas
What is wrong with my javascript recursion loop?

This is a code I am writing for leetcode question 46. I implemented the same logic in Python and it worked, but when I tried to write a JS equivalent of it, it doesn't work as expected. The for loops are not running for some reason.

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
const permute = nums => {

    const final_list = [];
    
    const bfs = (num, new_nums, current_list) => {
        current_list.push(num);
        if(new_nums.length === 0){
            final_list.push(current_list);
            return;
        }
        // This loop does not run for all iterations
        for(i=0;i<new_nums.length;i++){
            let new_nums2 = new_nums.filter(n => n!== new_nums[i]);
            bfs(new_nums[i], [...new_nums2], [...current_list]);
        }
    }
    // Only the runs for i=0    
    for(i=0;i<nums.length;i++){
        let new_nums = nums.filter(n => n!== nums[i]);
        bfs(nums[i], new_nums, []);
    }
    
    return final_list  
};

Input: [1,2,3] Output: [[1,2,3]]

I know the problem is with the loops because I console logged the loops and it does not fully run. In the bottom loop for example, it only ran for i=0. What am I doing wrong here?

Edit: Thanks to @Bergi I found out what's wrong, I missed the declaration for I. I don't think the flagged duplicate question is very helpful here though, just by looking at the so-called duplicate question I would never have known where I went wrong.

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