0

The issue I am having with the following javascript function is that it is returning the string value for mostpopular as "not a programmer", so although it is performing the correct operations where my console.log command is placed its return the mostpopular variable that hasn't been modified. if I am modifying the variable at the top at why wont it return the modifications... Its almost like javascript makes an instance and it only works in the local setting of the test function(if I place the console.log statement it prints out the correct data). Why is this ?

var mostPopular = "not a programmer";
var totalResults = 0;


function myfunction() {
    var listOfLanguages = ["Java", "C", "C++", "PHP", "C#", "Visual Basic", "Python", "Objective-C", "Perl", "Javascript", "Ruby"];



    for (var i = 0; i < listOfLanguages.length - 1; i++) {
        chrome.history.search({
            text: listOfLanguages[i],
            maxResults: 100
        }, function (search_results) {

            var countOfResults = search_results.length;
            var langOfResults = listOfLanguages[i - 1];

            test(countOfResults, langOfResults);

        });

    }

    console.log(mostPopular);
}

function test(count, lang) {


    if (count > totalResults) {

        totalResults = count;
        mostPopular = lang;
    }

}

window.onload = myfunction;
3
  • you don't want i < length - 1 for your loop; you just want i < length Commented Oct 8, 2013 at 16:48
  • 3
    chrome.history.search is async Commented Oct 8, 2013 at 16:49
  • You need to understand the call back functions from Javascript Commented Oct 8, 2013 at 16:50

1 Answer 1

2

As mentioned above the history search is Asynchronous and hence it requires a callback execution when completed

It will Work like below:

var mostPopular = "not a programmer";
var totalResults = 0;


function myfunction() {
    var listOfLanguages = ["Java", "C", "C++", "PHP", "C#", "Visual Basic", "Python", "Objective-C", "Perl", "Javascript", "Ruby"];



    for (var i = 0; i < listOfLanguages.length - 1; i++) {
        chrome.history.search({
            text: listOfLanguages[i],
            maxResults: 100
        }, function (search_results) {

            var countOfResults = search_results.length;
            var langOfResults = listOfLanguages[i - 1];

            test(countOfResults, langOfResults);
            console.log(mostPopular);
            // The Code will execute whenever the history search results are returned
        });

    }
// Any code here will be exceuted irrespective of the history search completed or not

}

function test(count, lang) {


    if (count > totalResults) {

        totalResults = count;
        mostPopular = lang;
    }

}

window.onload = myfunction;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.