My current project involves using Google's AJAX Language API to translate each element in an array.
for(var i=0; i < mytext.length; i++) {
google.language.translate(mytext[i], originalLanguage, newLanguage, function(result){
if(!result.error){
document.getElementById("caption") += mytext[i]+" has been translated to "+result.translation;
}
})
}
While this code successfully translates the entire array, I have encountered an issue where the success function called by google.language.translate always assigns n as equal to mycaptions.length. As a result, mycaptions[n] returns undefined (e.g., " has been translated to Hola"). This problem has been causing confusion for days (why does the value of n inside the callback function behave as if it's at the end of the loop???), and I suspect that the solution lies in a simple programming concept that eludes me.