0
votes

The following code is to add the font tag to every character, but it don't work

Function AddFontTag (counter)
    Do While Len (counter) < 7
        newStr = newStr & "<font>" & Left (counter, i) & "</font>"
        i = i + 1
    Loop
    AddFontTag = newStr
End Function

Because i'm not skilled in classic asp, such as variable scope, syntax. Does anyone know what's the problems of the above code?

thanks

3
What are you trying to do with this function? If you update your question with an output example (what you expect the function to output) it may be possible to give you a better answer. - xxbbcc

3 Answers

2
votes

Your do..while loop is an infinite loop - assuming counter is a string variable, its length never changes so if Len(counter) is less than 7 upon entering the function, it'll always stay less than 7 so your function never exits.

Your newStr variable is undefined - this works in VBScript but it's really bad practice and it's a source of countless errors. Is it a global variable or is it supposed to be a local? (It looks like a local.)

1
votes

I'm not sure of how your 7 character limit applies, but for a general approach the following will do what you need for any length string:

function AddFontTag(byval str)
    AddFontTag = Empty
    do while len(str) <> 0
        ' get next character
        dim c: c = left(str, 1)
        ' reduce original string
        str = right(str, len(str) - 1)
        ' build up output string
        AddFontTag = AddFontTag & "<font>" & c & "</font>"
    loop
end function

The example

dim test: test = AddFontTag("a test")
Response.Write test

will give you

<font>a</font><font> </font><font>t</font><font>e</font><font>s</font><font>t</font>

If you only want to apply this to strings of length less than 7 you can add

if len(str) > 6 then
    exit function
end if

before the while loop or

str = left(str, 6)

if you just want to apply it to the first 6 characters of any length string

0
votes

your codes is looping without a condition to EXIT the looping. try this... hope it helps.

Function AddFontTag (counter)
dim i,newStr,max
max=7
    Do While Len (counter) < max
        newStr = newStr & "<font>" & Left (counter, i) & "-" & "</font>"
        i = i + 1
        if i = max-1 then exit Do
    Loop
    AddFontTag = newStr
End Function
'to check the results
response.write AddFontTag ("params")

you will get <font>-</font><font>p-</font><font>pa-</font><font>par-</font><font>para-</font><font>param-</font>