I have following recursive method. It adds all individual integers in a numeric and returns an integer.
With input “123456789” the AddUp value becomes “9” (after a recursive call) checked in the debugger. But the method returns 0. How is that possible?
Function NineProof(Token as Integer) As Integer
'Convert numeric to string for counting
Var StringToken As String
Var Addup As Integer
StringToken = Token.ToString
'add every single integer
For I As Integer = 0 To StringToken.Length-1
Addup = Addup + StringToken.Middle(I,1).ToInteger
Next
If AddUp > 9 Then 'total is more than 1 integer
Token = NineProof(AddUp)
Else
Return AddUp
End if
End Function
3 posts - 2 participants