Skip to content

Commit 27c7b38

Browse files
committed
docs: Add detailed comments explaining CalculateCheckDigits array indexing
- Clarifies that number[11-n] indexing is safe for 11-element arrays - Documents the algorithm processes digits right-to-left with correct weights - Addresses potential confusion about array bounds in code review
1 parent 5b47f84 commit 27c7b38

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

MyNumberNET/MyNumber.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ public static int CalculateCheckDigits(int[] number)
4040
throw new MyNumberMalformedException("Malformed sequence. Must be 11 digits.");
4141
if (Array.Exists(number, n => n < 0 || n > 9))
4242
throw new MyNumberMalformedException("All digits must be between 0 and 9.");
43+
44+
// Calculate check digit using the official My Number algorithm
45+
// Process digits from right to left with specific weights
46+
// Array indexing: number[11-n] safely accesses indices 10,9,8,...,0 for n=1,2,3,...,11
47+
// This avoids Array.Reverse() while maintaining correct algorithm behavior
4348
var sum = 0;
49+
// First loop: rightmost 6 digits (indices 10,9,8,7,6,5) with weights 2,3,4,5,6,7
4450
for (var n = 1; n < 7; n++)
4551
sum += (n + 1) * number[11 - n];
52+
// Second loop: leftmost 5 digits (indices 4,3,2,1,0) with weights 2,3,4,5,6
4653
for (var n = 7; n < 12; n++)
4754
sum += (n - 5) * number[11 - n];
4855
if (sum % 11 <= 1)

0 commit comments

Comments
 (0)