Looping thru array, passing data to function

I'm having some trouble getting this to work.

var Browser = require('zombie');
var browser = new Browser({
 debug: true
})



function getPredictions(){
    var prediction = ['5', '7', '9', '11', '14', '18'];
    for(i in prediction){
        sendPrediction(prediction[i]);
    }
}

function sendPrediction(prediction){
    browser.visit('http://localhost:3000/prediction.php', function (error, browser){
        browser.fill('#prediction', prediction);
        browser.pressButton('Send', function (error, browser){
            if(browser.html == 'correct'){
                console.log('The correct prediction is ' + prediction +'');
            }else{
                console.log('The prediction ' + prediction + ' is incorrect.');
            }
        });
    });
}

getPredictions();

Basically, all four predictions I pass over from the array to my server, I want to be able to check if it's the correct prediction. '9' is the correct prediction, however it tells me that all of them are invalid even if browser.html is 'correct'.

How can I get this to work? What am I doing wrong?

I think your are reusing a same instance of zombie-browser. Try to rewrite your code this way. Now the getPrediction method will "wait" until previous one is completed and parsed (note the next argument).

function getPredictions(){
    var i = -1, prediction = ['5', '7', '9', '11', '14', '18'];
    var next = function() {
        i++;
        if(i < prediction.length)
            sendPrediction(prediction[i], next);
    }
    next();
}

function sendPrediction(prediction, next){
    browser.visit('http://localhost:3000/prediction.php', function (error, browser){
        browser.fill('#prediction', prediction);
        browser.pressButton('Send', function (error, browser){
            if(browser.html == 'correct'){
                console.log('The correct prediction is ' + prediction +'');
            }else{
                console.log('The prediction ' + prediction + ' is incorrect.');
            }
            next();
        });
    });
}

Also you may try to create a new Browser instance each time you checking a prediction

function sendPrediction(prediction){
    var browser = new Browser({ debug: true });
    browser.visit('http://localhost:3000/prediction.php', function (error, browser){
        browser.fill('#prediction', prediction);
        browser.pressButton('Send', function (error, browser){
            if(browser.html == 'correct'){
                console.log('The correct prediction is ' + prediction +'');
            }else{
                console.log('The prediction ' + prediction + ' is incorrect.');
            }
        });
    });
}