Copy link to clipboard
Copied
For some reason my .repeat isnt working for a text string.
If i wanted five 0's
I wanted '0'.repeat(5);
not working.
any help is appreciated.
It would be best if you rewrote that repeat function as a separate method, so you could simply use this:
function repeatString(string, times) {
var result = '';
for (var i = 0; i < times; i++) {
result += string;
}
return result;
}
Copy link to clipboard
Copied
In what context are you using this method? C++, Expression, CEP extension, or ExtendScript?
If it's ExtendScript, then this method will not work, since ExtendScript uses very old JavaScript version (ECMA version 3.0) that does not have this method. You'll have to use the for loop to concatinate strings to achive what you are afterr.
Copy link to clipboard
Copied
Thank you. Yes. Extendscript. This makes sense. I'll jump in and give it a shot.
Copy link to clipboard
Copied
mName = "My_Diffuse_[00000-00030].png" //establish name
numSlice = mName.lastIndexOf("["); //get number index
numSlice2 = mName.lastIndexOf("-"); //get number index
zeroAmount = numSlice2-numSlice; // calculate amount of characters needed
sName = mName.slice(0, numSlice); //slice mName based on numSlice. I remove the "[00000-00030].png"
//this for loop i wat to add the proper amount of 0's at the end of the sliced file name
for (i = 1; i < zeroAmount; i++){
sName.concat("0");
}
//show me result.
alert(sName);
Copy link to clipboard
Copied
Above is my latest. I think im doing things right, its just not working.
Copy link to clipboard
Copied
Your for loop is incorrect. Try this instead:
for (i = 1; i < zeroAmount; i++){
sName += "0";
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
It would be best if you rewrote that repeat function as a separate method, so you could simply use this:
function repeatString(string, times) {
var result = '';
for (var i = 0; i < times; i++) {
result += string;
}
return result;
}
Copy link to clipboard
Copied
A @Tomas Sinkunas says.
Here you find a polyfill:
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
You can copy and paste the code of the polyfill in your program and it will add the missing repeat method.