Coverting javascript to vbscript

Status
Not open for further replies.

Uchiha

Ars Scholae Palatinae
807
Hey guys,

This if for homework. So there is a textbox with 10 words in it. I am trying to find how many words in the string named ta_val. I already did the javascript but need help in vbscript. Any converter programs exist? Anyways any help is appreciated.

ta_val=document.tb.ta.value;
len_ta=ta_val.length;

var sArray = ta_val.split("");
var sLength = sArray.length;
a.innerHTML = sLength;


I tried this and it did not work.

ta_val=document.tb.ta.value
len_ta=len(ta_val)

dim sArray = split(ta_val,"")
dim sLength = len("sArray")
a.innerHTML = sLength
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
moniker117":47h9wift said:
I haven't really tested any of the code, but I'm surprised the javascript code works. Anyway, in the vbscript, when you wrote len("sArray") don't you mean len(sArray)?

Yes, that's the only thing where he went wrong in syntax conversion.
As for the question about converter programs, I'd recommend "notepad.exe". It does a great job, but you'd need to configure (type) a lot yourself ;)
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
Well, I d suggest trying to see
- if your code generates errors that make it uninterpretable or not able toe be executed
- what happens while it gets executed.

So, first go to "menu/tools/internet options/tab advanced" and make sure that you
- uncheck "disable script debugging (Internet Explorer)"
- check "display a notification about every script error"
That will alert you to anything that goes wrong instead of ignoring it and hiding from the user

Also to set your breakpoint at the first line of your code:
go to "menu/tools/developer tools/ tab script". That's where your code should be, and that's where you can place breakpoints.
 
Uchiha":1dkytt9t said:
Thanks guys, I did delete the quotes but it still does not work. I am actually using notepad for this and saving to .htm.

You're welcome. :) Too bad it did not fix the code.

Are you sure the javascript works? I still haven't tested any of the code, and I'll confess that I'm not a javascript or vbscript expert :), but I don't see how it can work.

Looking at the vbscript code:

Code:
dim sArray = split(ta_val,"")

What's this line all about?? Split, I imagine, divides the string in ta_val into an array of strings based on your delimiter, "", and puts them in sArray. Your delimiter is empty string, am I reading this right? Looks like the javascript code is doing the same thing, but I don't understand why.
 

Delirious

Wise, Aged Ars Veteran
172
I would think (without checking this) that the referencing of elements is slightly different for VBScript to JavaScript. I always use document.getElementById in JavaScript, and I haven't referenced any element in VBScript (I just tend to use it for work stuffs like getting system variables (i.e. %USERNAME%).

In short: Double check referencing elements in VBScript. Otherwise, no idea what's up with your code.
 

Delirious

Wise, Aged Ars Veteran
172
Uchiha":3fxojblo said:
ta_val=document.tb.ta.value;
len_ta=ta_val.length;

var sArray = ta_val.split("");
var sLength = sArray.length;
a.innerHTML = sLength;


I tried this and it did not work.

ta_val=document.tb.ta.value
len_ta=len(ta_val)


dim sArray = split(ta_val,"")
dim sLength = len("sArray")
a.innerHTML = sLength

Try:
Dim ta_val = document.tb.ta.value
Dim len_ta =len(ta_val)
??
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
moniker117":3bzq275d said:
Hmm... I see. The issue is with these two lines:

JavaScript
Code:
var sLength = sArray.length;

VBScript
Code:
dim sLength = len(sArray)

They don't mean the same thing in this context.

Oh my, it's been such a long time I did not see that one at first :eek:
You're right, the equivalent VbScript should be something like this
VBScript
Code:
dim sLength = Ubound(sArray) - Lbound(sArray)
 

wb

Ars Legatus Legionis
25,208
Subscriptor
Delirious":102ay6li said:
Dim is use to declare any variables, you may be correct in saying that you can't assign as well as dim but you could dim your vars and then assign them

Yes what I am saying is that the lines that people keep recommending are not legal syntax and will not work.

Also in vbscript I believe you have no choice in setting the index like in VB, so you can assume it is always zero and then you get:

Code:
dim sArray, sLength
sArray = split(ta_val, "")
sLength = UBound(sArray)
 
The following code should do what you want:

Code:
dim sArray
sArray = split(ta_val," ")
dim sLength
sLength = Ubound(sArray)+1

First, every reply I've seen shows an example that is split on an empty string or "". This won't work. You have to split on a space, or " ". This assumes you're getting your text box value correctly. Len is a string function, you need to use Ubound to get the upper bound index of the array. Add one to it since it's 0 based and that is your word count.
 
Status
Not open for further replies.