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.119 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.118 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.117 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.116 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.115 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.114 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.113 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.111 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.110 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.109 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.108 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.107 tracks/javascript/exercises/luhn/example.js
trackler-2.2.1.106 tracks/javascript/exercises/luhn/example.js