I need to find the number of 3, 4, 5 and 6 letter words in a string using VBScript -
here's question have answer assignment:
count number of words in string "tx_val" have 3,4,5 or 6 chatacters. show these 4 counts on single line seperated commas in span block id="ans12"
.
here's i've come with, output incorrect , i'm not sure why. i'll post below. thought i'd give update of @ it.
threematch = 0 fourmatch = 0 fivematch = 0 sixmatch = 0 totalmatch = "" cntarr = array() cntarr = split(tx_val," ") i=0 i=0 ubound(cntarr) step 1 if len(cstr(cntarr(i))) = 3 threecount = threecount + 1 elseif len(cstr(cntarr(i))) = 4 fourcount = fourcount + 1 elseif len(cstr(cntarr(i))) = 5 fivecount = fivecount + 1 elseif len(cstr(cntarr(i))) = 6 sixcount = sixcount + 1 end if i=i+1 next totalmatch = (threecount & ", " & fourcount & ", " & fivecount & ", " & sixcount & ".") document.getelementbyid("ans12").innerhtml = totalmatch
first, , causing wrong behaviour, explicitly incrementing counter i
, though for-next
loop you. result each pass through loop, i
gets incremented 2.
remove i=i+1
line , script work intended.
second, variable names inconsistent, being initialised e.g. threematch
, later used threecount
. should declare variables explicitly (dim
statements) , write option explicit
@ top of code catch such obvious mistakes @ compile time. pure chance mistake not cause errors in particular case.
Comments
Post a Comment