Sha256: 381140b8ec558deeb800bb349b0086b4adce5f3c5a06ed078e950cc7f5f5fb7a

Contents?: true

Size: 643 Bytes

Versions: 73

Compression:

Stored size: 643 Bytes

Contents

'use strict';

function isValid(number) {
  var numbers = number.replace(/\s/g, '');
  const digits = [...numbers];

  const sum = digits
    // convert to integers
    .map(d => parseInt(d, 10))
    // double even positions (odd indexes)
    .map((d, i) => {
      if (i % 2 !== 0) {
        return d * 2;
      }
      return d;
    })
    // limit to digits less than 10
    .map(d => {
      if (d > 9) {
        return d - 9;
      }
      return d;
    })
    // sum all digits
    .reduce((d, acc) => d + acc, 0);

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

function Luhn(number) {
  this.valid = isValid(number);
}

module.exports = Luhn;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.138 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.137 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.136 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.135 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.134 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.133 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.132 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.131 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.130 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.129 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.128 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.127 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.126 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.125 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.124 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.123 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.122 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.121 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.120 tracks/javascript/exercises/luhn/example.js