Here’s the question I have to answer for my assignment:
Count the number of words in the string “tx_val” that have 3,4,5 or 6 chatacters. Show these four counts on a single line seperated by commas in the span block id="ans12".
Here’s what I’ve come up with, the output is incorrect and I’m not sure why. I’ll post below. Thought I’d give you all a update of where I was at with it.
threematch = 0
fourmatch = 0
fivematch = 0
sixmatch = 0
totalmatch = ""
cntArr = Array()
cntArr = Split(tx_val," ")
i=0
For i=0 To Ubound(cntArr) Step 1
If len(cstr(cntArr(i))) = 3 Then
threecount = threecount + 1
ElseIf len(cstr(cntArr(i))) = 4 Then
fourcount = fourcount + 1
ElseIf len(cstr(cntArr(i))) = 5 Then
fivecount = fivecount + 1
ElseIf len(cstr(cntArr(i))) = 6 Then
sixcount = sixcount + 1
End If
i=i+1
Next
totalmatch = (threecount & ", " & fourcount & ", " & fivecount & ", " & sixcount & ".")
document.getElementById("ans12").innerHTML = totalmatch
First, and this is what is causing the wrong behaviour, you are explicitly incrementing your counter
i, even though theFor-Nextloop already does that for you. The result is that for each pass through the loop,iactually gets incremented by 2.Remove the
i=i+1line and your script will work as intended.Second, your variable names are inconsistent, being initialised as e.g.
threematchand later used asthreecount. You should always declare your variables explicitly (Dimstatements) and writeOption Explicitat the top of your code to catch such obvious mistakes at compile time. By pure chance this mistake does not actually cause any errors in your particular case.