|
-
Feb 29th, 2008, 07:44 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] VB6 to Javascript
I've created some code in VB6 and was wondering if anyone could help me with converting it to Javascript.
My main concern is with arrays, i'm not too sure how to use them in javascript, as you will see.
Here's what i've converted so far (Bolded lines i have not converted, or are unsure about):
Code:
var primefactors[0]
function prime_calculation(num_)
{
var F_;
var N_;
var C_;
F_ = 2;
N_ = num_;
Do
{
If (divide_evenly(N_, F_)==False);
{
F_ = F_ + 1;
}
else
{
If (C_ >= 1)
{
ReDim Preserve PrimeFactors(UBound(PrimeFactors) + 1);
}
PrimeFactors(UBound(PrimeFactors)) = F_ ;
N_ = parseInt(N_ / F_) ;
}
C_ = C_ + 1;
}
while (N_>1)
check_array();
return construct_number_string()
}
function divide_evenly(num1,num2)
{
If (Num1 / Num2 == parse.Int(Num1 / Num2))
{
return 1
}
Else
{
return 0
}
}
function check_array()
{
var TmpArr[];
var I_;
For I_ = 0 To UBound(PrimeFactors)
If (PrimeFactors(I_) == 0)
{
removeitem (primefactors(), I_);
}
Next I_
}
function removeitem(sArray[],index)
{
var K;
If (Index < UBound(sArray))
{
For K = Index To UBound[sArray] - 1
sArray[K] = sArray[K + 1];
Next K
}
ReDim Preserve sArray(UBound[sArray] - 1)
}
function construct_number_string()
{
var I_;
var tmpstr;
For I_ = 0 To UBound[PrimeFactors]
tmpstr = tmpstr + primefactors[I_] + ", ";
Next I_
return tmpstr
}
}
Here is the orginal VB6 Code (Please note that i have taken some stuff out, such as the 'on error resume next' and that):
Code:
Option Explicit 'Makes sure there's nothing undefined
'Declare program class private variables
Dim PrimeFactors() As Integer 'Main array for storing prime integers
Private Function Prime_Calculation(Num_ As Integer) As String
On Error GoTo Errors 'If there's an error go to the error handler label
'There shouldn't be any errors! But meh.
'Declare private variables
ReDim PrimeFactors(0)
Dim F_ As Integer
Dim N_ As Integer
Dim C_ As Integer 'Used for counting how many times the loop has occured
'Set standard variable integers
F_ = 2
N_ = Num_
Do Until N_ <= 1 'Start the loop for discovering the prime numbers
If Divide_Evenly(N_, F_) = False Then 'If they don't divide evenly then incease F_
F_ = F_ + 1
Else 'Or if they do divide evenly
'Check to see if it's not the first time the loop has happened (Logic errors will happen
'when the array is checked, and strange prime numbers will occur, some prime numbers
'will also be missing), Then increase the array's dimensions by 1.
If C_ >= 1 Then ReDim Preserve PrimeFactors(UBound(PrimeFactors) + 1)
PrimeFactors(UBound(PrimeFactors)) = F_ 'Place the data (F_) into the last array index
N_ = Int(N_ / F_) 'Divide N_ by F_ and make the answer N_
End If
C_ = C_ + 1 'Increase the loop count by 1
Loop 'Loop back to the top
Check_Array 'Check the array for any anomalies
Prime_Calculation = Construct_Number_String 'Construct the string and pass it to the calling sub
Exit Function 'Exit the function (Will go to the error handler otherwise)
Errors: 'Error handler label
MsgBox "You are experiencing a mysterious error that shouldn't exist." 'Have a laugh.
Exit Function 'Exit the function so the program won't crash
End Function
Private Function Divide_Evenly(Num1 As Integer, Num2 As Integer) As Boolean
'Checks to see if the rounded number is the same as the non-rounded number
If Num1 / Num2 = Int(Num1 / Num2) Then
Divide_Evenly = True
Else
Divide_Evenly = False
End If
End Function
Private Sub Check_Array()
On Error Resume Next 'If there's an error continue to the next line ignoring the error line
'Declare private variables
Dim TmpArr() As Integer
Dim I_ As Integer
'If 0 is present in the array, remove that array index
For I_ = 0 To UBound(PrimeFactors)
If PrimeFactors(I_) = 0 Then RemoveItem PrimeFactors(), I_
Next I_
End Sub
Private Sub RemoveItem(sArray() As Integer, ByVal Index As Integer)
'Thanks to Static on VBforums for this sub's code
'(http://www.vbforums.com/showthread.php?t=451353&highlight=remove+array+index)
Dim K As Integer
If Index < UBound(sArray) Then
For K = Index To UBound(sArray) - 1
sArray(K) = sArray(K + 1)
Next K
End If
ReDim Preserve sArray(UBound(sArray) - 1)
End Sub
Private Function Construct_Number_String(Optional Before_ = "", Optional After_ = ", ", Optional Include_ = False) As String
'Before_ is any formatting that goes before the number
'After_ is any formatting that goes after the number (Default is ", ", Comma and a space)
'Include_ is whether the user wants the last number to have the formatting on the end (True), or not (False)
'Declare private variables
Dim I_ As Integer
Dim TmpStr As String
For I_ = 0 To UBound(PrimeFactors) 'Go through every index
If I_ = UBound(PrimeFactors) Then 'If at the end of the array then
If Include_ = True Then 'Check to see if Include has been enabled
TmpStr = TmpStr & Before_ & PrimeFactors(I_) & After_ 'Place the formatting on the end
Else 'Or if Include is not enabled
TmpStr = TmpStr & Before_ & PrimeFactors(I_) 'Ommit the formatting
End If
Else
TmpStr = TmpStr & Before_ & PrimeFactors(I_) & After_ 'Continue constructing the string
End If
Next I_ 'Do the next index in the array
Construct_Number_String = TmpStr 'Place the temporary string into the sub to be transfered into the calling sub
End Function
Private Sub CmdCalculate_Click()
On Error GoTo Errors 'If there's an error go to the error handler label
'Check if the number is 4 or larger (There is no use for it to be smaller)
If PrimeTxt.Text <= 3 Then
MsgBox "Please enter a number larger then 4."
Exit Sub
End If
'Integers can only take numbers up to 32767 before an overflow happens. Stop the user from causing this error
If PrimeTxt.Text >= 32768 Then
MsgBox "Please enter a number less then 32768."
Exit Sub
End If
AnswerTxt.Text = Prime_Calculation(PrimeTxt.Text) 'Display the data
Exit Sub 'Exit the sub (Will go to the error handler otherwise)
Errors: 'Error handler label
MsgBox "The number that you have inputted is invalid." 'Inform the user of the error
Exit Sub 'Exit the sub so the program won't crash
End Sub
Any help will be appreciated (I don't expect you to solve it all for me, but rather point out errors, and me in the right direction).
I will fix up the little errors (Such as cases not matching and that) once i have properly translated it into javascript.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|