Appending String Lengths
JavaScript (ES6), 32 bytes
f=(s,n=0)=>(s+n)[n]?f(s,n+1):s+n
How it works
f = (s, n = 0) => // given a string 's' and starting with n = 0:
(s + n)[n] ? // if the Nth character of (s + n) exists:
f(s, n + 1) // try again with n + 1
: // else
s + n // return s + n
Starting with N=0
, we test the Nth character (0-based) of the string made of the concatenation of the original input string and the decimal representation of N
. We increment N
until this character doesn't exist anymore.
Example:
N = 0 : abcdefghi0
^
N = 1 : abcdefghi1
^
N = 2 : abcdefghi2
^
...
N = 8 : abcdefghi8
^
N = 9 : abcdefghi9
^
N = 10 : abcdefghi10
^
N = 11 : abcdefghi11 -> success
^
Test cases
f=(s,n=0)=>(s+n)[n]?f(s,n+1):s+n
console.log(f("aaa")); // -> aaa4
console.log(f("")); // -> 1
console.log(f("aaaaaaaa")); // -> aaaaaaaa9
console.log(f("aaaaaaaaa")); // -> aaaaaaaaa11
console.log(f("a1")); // -> a13
LaTeX, 108/171
\newcounter{c}\def\s#1#2]{\stepcounter{c}\def\t{#2}\ifx\empty\t\arabic{c}\else#1\s#2]\fi}\def\q[#1]{\s#10]}
\q[] //1
JavaScript (ES6), 37 bytes
f=(s,t=s,u=s+t.length)=>t==u?t:f(s,u)
<input oninput=o.textContent=f(this.value)><pre id=o>