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

267
Views
(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
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!