Socialify

Folder ..

Viewing luhnCheck.js
22 lines (22 loc) • 495.0 B

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
module.exports = function (number) {
  number = number.replace(/\D/g, "");
  var split = number.split("");
  split = split.map(function (num) {
    return parseInt(num);
  });
  var check = split.pop();
  split.reverse();
  split = split.map(function (num, index) {
    if (index % 2 === 0) {
      num *= 2;
      if (num > 9) {
        num -= 9;
      }
    }
    return num;
  });
  var sum = split.reduce(function (prev, curr) {
    return prev + curr;
  });
  return sum % 10 === check;
};