Skip to content

Instantly share code, notes, and snippets.

@oPorks
Forked from ShirtlessKirk/luhn.js
Created July 28, 2014 14:08
Show Gist options
  • Select an option

  • Save oPorks/2534a823bac5ee5a00d2 to your computer and use it in GitHub Desktop.

Select an option

Save oPorks/2534a823bac5ee5a00d2 to your computer and use it in GitHub Desktop.

Revisions

  1. @ShirtlessKirk ShirtlessKirk revised this gist Jul 16, 2014. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions luhn.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    // Variant of Avraham Plotnitzky's String.prototype method mixed with the "fast" version
    // see: https://sites.google.com/site/abapexamples/javascript/luhn-validation
    /**
    * Variant of Avraham Plotnitzky's String.prototype method mixed with the "fast" version
    * see: https://sites.google.com/site/abapexamples/javascript/luhn-validation
    * @author ShirtlessKirk. Copyright (c) 2012.
    * Licensed under WTFPL (http://www.wtfpl.net/txt/copying)
    */

    function luhnChk(luhn) {
    var len = luhn.length,
  2. Phil Green renamed this gist Mar 20, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Phil Green revised this gist Mar 20, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    // Variant of Avraham Plotnitzky's String.prototype method mixed with the "fast" version
    // see: https://sites.google.com/site/abapexamples/javascript/luhn-validation

    function luhnChk(luhn) {
    var len = luhn.length,
    mul = 0,
  4. Phil Green created this gist Mar 20, 2012.
    13 changes: 13 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    function luhnChk(luhn) {
    var len = luhn.length,
    mul = 0,
    prodArr = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]],
    sum = 0;

    while (len--) {
    sum += prodArr[mul][parseInt(luhn.charAt(len), 10)];
    mul ^= 1;
    }

    return sum % 10 === 0 && sum > 0;
    };