0

I'm a little bit confused by what my code showed me in the terminal.

I have this for loop. It just counts from 8 to 0 - nothing special - in each loop it should do some "if, else" and log the result to the console. Because it didn't worked i printed the count-variable.

What i expected was something like:

i: 8 result, i: 7 result ...

Reality:

i: 8 i: 7 i: 6 ... and after that... result result result ...

for(var i = 8; i >= 0; i--) {
    console.log('i: ',  i);

    if(searchSeq(tempPruefanw, i) == 'found sth') {
        console.log('pruefanweisung: ', pruefanweisung);
        res.send(pruefanweisung);
        break; // break for
    } else {
        // nothing found at all
        pruefanweisung =  "no file found";
    }
}

Why is the logging output like this and can I assume that my code is working like the output is tracked? - I mean then it would do every line of code 9 times and not like i expected block after block or is this just the style of console.log? What is the routine of JavaScript to handle functions? - i thought step by step ...

At the same line where the if-statement starts i've written a comparison between a function result and a string. Unfortunately the result of searchSeq(tempPruefanw, i) is undefined. I've read some articles about "undefined" in JavaScript but nothing helped me with my problem. searchSeq() should just return a string - nothing special. The function works fine, it logs everything it does but then it returns undefined. In all of my other functions the return-thing worked for me as expected. After some googling i found out that the problem could be that my return is not in the main-function. I interpreted main-function as searchSeq(). I tried to give a var res; directly under var searchFile;, then i assigned the result und tried to return this result-variable after the closing bracket of find.file(). The outcome was the same. Undefined.

function searchSeq(tempFileName, i) {
    var searchFile = tempFileName.replace(tempFileName[11], i);

    find.file(searchFile, __dirname, function(files) {
        if(files.length <= 0) {
            console.log('No file found for: ', searchFile);
            return 'nth found';
        } else {
            var filePath = editPath(path.resolve(files[0]));
            console.log('File found for: ', filePath);
            pruefanweisung = filePath;
            return 'found sth';
        }
    });
}

Why did i receive an undefined result instead of the expected string? How can I fix this?

Milo
  • 23
  • 2
  • 11
  • I assume the `res.send()` function is async. Hence, the execution of the for loop does not wait for that operation to be finished and you'll get the output for all for-loop-entries before getting the first of the `send()` function. `searchSeq()` suffers from a similar problem. See https://stackoverflow.com/q/14220321/1169798 for solutions. – Sirko Feb 13 '18 at 08:20
  • Also see https://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found/42913882#42913882 You shouldn't set it to `no file found` inside the loop, only when the loop is done. – Barmar Feb 13 '18 at 08:22

0 Answers0