简体   繁体   中英

Javascript while or for loop that changes a string until a condition is met

I am checking to see if a file exists and if so I want to do a javascript loop until the file name is okay to use.

I can't figure it out(var james is defined beforehand):

var i = 1;
while (filexisto(james+".ogg")) {
  james = james+"_"+i;
  i++;                           
}

For example, I want it to give me file names like this:

james.ogg

james_1.ogg (james.ogg exists)

james_2.ogg (james.ogg exists and james_1.ogg exists)

james_3.ogg etc...

right now it will do james_12.ogg if (james.ogg exists and james_1.ogg exists). How can I structure my loop to give me what I want?

Why not use another variable to hold the original "james" string?

var i = 1;
var JAMES = james;
while (filexisto(james  +".ogg")) {
  james = JAMES+"_"+i;
  i++;
}     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM