File: luhn_algorithm.coffee

package info (click to toggle)
coffeescript 1.10.0~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,292 kB
  • sloc: makefile: 62
file content (36 lines) | stat: -rw-r--r-- 827 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Use the Luhn algorithm to validate a numeric identifier, such as credit card
# numbers, national insurance numbers, etc.
# See: http://en.wikipedia.org/wiki/Luhn_algorithm

is_valid_identifier = (identifier) ->

  sum = 0
  alt = false

  for c in identifier by -1

    # Get the next digit.
    num = parseInt c, 10

    # If it's not a valid number, abort.
    return false if isNaN num

    # If it's an alternate number...
    if alt
      num *= 2
      num = (num % 10) + 1 if num > 9

    # Flip the alternate bit.
    alt = !alt

    # Add to the rest of the sum.
    sum += num

  # Determine if it's valid.
  sum % 10 is 0


# Tests.
console.log is_valid_identifier("49927398716")      is true
console.log is_valid_identifier("4408041234567893") is true
console.log is_valid_identifier("4408041234567890") is false