Question: So I have the following code

```
var processed;
fs.readFile(path, 'utf-8', function(err, data) {
    processed = false;
    //checking if text is in file and setting flag
    processed = true;
});

if (processed == true) {
    try {
        var fname = path.substring(path.lastIndexOf("\\") + 1);
        fs.moveSync(path, './processedxml/' + fname, {
            overwrite: true
        })
    } catch (err) {
        console.log("Error while moving file to processed folder " + err);
    }

}

```

But I don't get the desired output. Because looks like the readfile is executed by a separate thread and so the value of "processed" is not reliable.

I am not very familiar with nodejs so any help will be greatly appreciated.

Answer: 
For counting the **even digits** of an integer **base 10** you can simplify the function to the following

```
int countEven(int n)
{
    if (n != 0) return !(n % 2) + countEven(n/10);
    else        return 0;
}

```

This expands as follows. Assume `n = 258`:

```
            countEven(258) = 
1 +         countEven(25)  = 
1 + 0 +     countEven(2)   = 
1 + 0 + 1 + countEven(0)   = 2

```

Note that the statement `!(n % 2)` returns **1** if `n` is even and **0** if it's odd.

For shorter you can do the following:

```
int ce(int n) { return n ? !(n&1) + ce(n/10) : 0; }

```

using the [ternary operator](https://stackoverflow.com/questions/392932/how-do-i-use-the-conditional-operator).
seems like you're trying to count the even digits in a number

```
int countEven(int n){
    if(n == 0) 
        return 0; //base case
    if (n<10)
        return !(n%2);
    return !(n%2)+countEven(n/10);
}

```