Sha256: b54628e344abb8b6965827f30178b7d9a2c78f0021f2d5e6b5610f35d00f6706

Contents?: true

Size: 634 Bytes

Versions: 139

Compression:

Stored size: 634 Bytes

Contents

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

  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;
}

export default class Luhn {

  constructor(number) {
    this.valid = isValid(number);
  }

}

Version data entries

139 entries across 139 versions & 1 rubygems

Version Path
trackler-2.0.8.31 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.30 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.29 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.28 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.27 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.26 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.24 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.23 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.22 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.21 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.20 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.19 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.18 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.17 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.16 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.15 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.14 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.13 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.12 tracks/ecmascript/exercises/luhn/example.js
trackler-2.0.8.11 tracks/ecmascript/exercises/luhn/example.js