I am wondering if this is a feature of google apps script (or javascript as a whole for that matter) or a bug. I get a strange result when I call a function from inside a loop. Now, within the called function there is a loop that uses the same variable that the variable running on the calling loop and this seems to cause a problem. Here the code:
function dudi() {
var folderName='FormsGenerator';
var ss=new Array(2);
for(o=0;o<2;o++){
var str='dudo' + o;
trashFile(str,folderName);
ss[o]=SpreadsheetApp.create(str);
Logger.log(str);
}
}
function trashFile(fileName,folderName){
var folder=DocsList.getFolder(folderName);
var lFolder=folder.getFiles();
for(o=0;o < lFolder.length;o++){
if(lFolder[o].getName() == fileName) {
DocsList.getFileById(lFolder[o].getId()).setTrashed(true);
}
}
What happens is that the loop in the calling function stops after the first iteration. If in trashFile I change the loop index variable to "p" or I use a "var o=0" instead of a "o=0", the problem goes away. What am I doing wrong? Is this a well known feature or a bug? I have been programming in C and C++ for long years, but I am fairly new with javascript/GAS.
Max