This code is used to check the algorithm to see if the number is valid. It checks the nine digit number. First eight digits are used in calculation. You add 1st, 3rd, 5th and 7th digit and add them together. You take 2nd, 4th, 6th and 8th in the other part of calculation, multiply each of this digits by 2 and divide it by 10. Add quotient and remainder to get the value for each of the digits and add them together. Add these both total to get the total value and divide it by 10. 10 minus the remainder should be the last digit for the 9th digit to be valid.
Following Code is being tested:
Quote:var vTestString as String
Var i as Int
var vTotal1 as Int
var vTotal2 as Int
var vtemp as Int
var vFinalTotal as Int
var vResult as Int
Writeln ("The Number being tested for Validity is " + ThisElement)
//number validation for Medicaid - Algorithm
vTestString = @Left (ThisElement, 8 )
Writeln ("Eight Digits tested in calculation are " + vTestString)
vTotal1 = 0
vTotal2 = 0
vTemp = 0
For i = 8 downto 2 Step 2
vTemp = 0
vTemp = @Tonumber (@Mid (vTestString, i, 1))*2
vTemp = @int (vTemp/10) + @MOD (vTemp, 10)
WriteLn ( "vTemp = " + vTemp)
vTotal1 = vTotal1 + vTemp
vTotal2 = vTotal2 + @Tonumber (@Mid (vTestString, i-1, 1))
Next
vFinalTotal = vTotal1 + vTotal2
vResult = @Mod (vFinalTotal, 10)
Writeln ( "vTotal1 = " + vTotal1 + @newline ( ) + "VTotal2 = " + vTotal2 + @newline ( ) + "FinalTotal = " + vFinalTotal )
Writeln ("vResult = " + vResult )
WriteLn ("Last Digit should be " + @str(10 - vResult))
If @Right (ThisElement, 1) = @str(10 - vResult) then
{
Writeln ("The variables assignment worked Successfully")
}
Else
{
Writeln ("The Calculation was Erratic with the variable assigned")
}
170657613 is a valid number and it should pass through the test if calculation works right.
I shall present here the result of the test I have conducted while debugging the code.
==========================
When varialbe type assigned as follows:var vTestString as String
Var i as Int
var vTotal1 as Int
var vTotal2 as Int
var vtemp as Int
var vFinalTotal as Int
var vResult as Int
The result of the above codeThe Number being tested for Validity is 170657613
Eight Digits tested in calculation are 17065761
vTemp = 2
vTemp = 4
vTemp = 2
vTemp = 4
vTotal1 = 12
VTotal2 = 12
FinalTotal = 24
vResult = 3
Last Digit should be 7
The Calculation was Erratic with the variable assigned
============================
Please look at the value of vTemp variable, it failed to add the Quotient in the statement where @MOD is used.
Logically I thought, I should assign variable type Double where @MOD function is used as programming guide points out. That made me change last three variables to Double
--------------------------------------------------------
var vTestString as String
Var i as Int
var vTotal1 as Int
var vTotal2 as Int
var vtemp as Double
var vFinalTotal as Double
var vResult as Double
The Results of the assignment of above variable type:The Number being tested for Validity is 170657613
Eight Digits tested in calculation are 17065761
vTemp = 2
vTemp = 5
vTemp = 3
vTemp = 5
vTotal1 = 14
VTotal2 = 12
FinalTotal = 26
vResult = 6
Last Digit should be 4
The Calculation was Erratic with the variable assigned

===========================
It messed up when totaling value of vTemp, vTotal value is not totaled up right. It should be 15 but it missed 1 somewhere and gave value of 14, leading to erratic calculation. I assumed that probably this is due to variable mismatched. vTotal1 being Int type while vTemp1..vTemp4 are Double type. That lead me to change the other applicable variables to Double as follows.
---------------------------------------------------------
var vTestString as String
Var i as Int
var vTotal1 as Double
var vTotal2 as Double
var vtemp as Double
var vFinalTotal as Double
var vResult as Double
The Results of the assignment of above variable type:The Number being tested for Validity is 170657613
Eight Digits tested in calculation are 17065761
vTemp = 2
vTemp = 5
vTemp = 3
vTemp = 5
vTotal1 = 15
VTotal2 = 12
FinalTotal = 27
vResult = 7
Last Digit should be 3
The variables assignment worked Successfully
=============================
It worked!!!
This lead me to following conclusions:1. @MOD does not work reliably with Int variables.
2. When there are variable type mixing, Double and Int, in any mathematical statement, it might give unpredictable results.
3. I know very little about variable usage and need a lot of learning to understand Double and Int variables. 4. I need help understanding the fundamentals.5. Writeln is indispensable for debuging any code.
In the last If....then statement, I had rough time getting the proper result. After converting both the compared to string, it worked correctly. ???
The above code can be tested by anyone, just have two text elements, any names, just place the code in the first element on Element Exit Event. That's it. It can be tested in preview as it does not have any xfamily statement or function. Place the number to be tested for validity in the first element and hit tab, you will see the result.
The Programming is all about doing.....