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

269
Vistas
(ES6) Why does the value of variables return to initial value after it gets out of the loop?

I initialized two variables count and user, and I changed their value in the loop. Why does the function always return [0, 0] on finish?

Here's my code:

function getMonthSubs(month) { // month are the name of folders
    var cb = {count: 0, user: 0}, userTemp = [];
    readdir('./log/' + month, 'utf-8', function (err, files) { // files e.g. '2021_11_30.json'
        if (err) return [-1, -1]; 
        else {
            for (let j = 0; j < files.length; j++) {
                let temp = JSON.parse(readFileSync('./log/' + month + '/' + files[j], 'utf-8'));
                for (var key in temp) {
                    cb.count += temp[key].length;
                    if (!userTemp.includes(key)) {
                        userTemp.push(key);
                        cb.user++;
                    }
                }
            }
        }
    });
    return Array(cb.count, cb.user); // [0, 0]
}
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