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.2.0.0 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/luhn/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/luhn/example.js