Author Topic: Characters representing numbers  (Read 794 times)

Hey, is there an easier way than a massive case statement to make, for example, the variable equal to 2 come out as "oo" and the variable equal to 5 come out as "ooooo", etc?

Code: [Select]
function repeatCharacter(%char,%num)
{
for(%i=0;%i<%num;%i++)
%ret = %ret @ %char;
return %ret;
}
something like this you mean?

Code: [Select]
echo(repeatCharacter("o",5));

I don't know why I didn't think of that, I guess it's too elegant. Thanks :cookieMonster:

I don't know why I didn't think of that, I guess it's too elegant. Thanks :cookieMonster:
do you understand how all the code works?
I can explain any part if needed

Why does %ret have to equal %ret @ %char?

You're basically redefining %ret with the old value that has %char added on to it.

I see I see. The whole system works perfectly with this so that's a change for me :D Thanks for that

think of it like this any undefined variable starts as a blank string
so through each iteration
->repeatCharacter("o",5);
i = 0
  ret = "" @ "o";
i = 1
  ret = "o" @ "o";
i = 2
  ret = "oo" @ "o";
i = 3
  ret = "ooo" @ "o";
i = 4
  ret = "oooo" @ "o";
for loop break
ret now equals "ooooo";
return ret


the @ symbol concatenates strings/variables so
"ooo" @ "a" = "oooa"
or
"ooo" @ 5 = "ooo5"

intrinsically every iteration of the for loop you are setting ret to ret and then adding on a string

common concatenation options
@  combines two strings   "a" @ "o" = "ao"
SPC  combines two strings with a space in between "a" SPC "o" = "a o"
TAB  combines two strings with a tab in between "a" TAB "o" = "a\to" = "a   o"

That is SO clever and useful, brilliant