Sha256: 8c19e4cf656cbee7d4d204f8b076e8c855b47740c0554fab793e4de20aa0df63

Contents?: true

Size: 1.34 KB

Versions: 72

Compression:

Stored size: 1.34 KB

Contents

'use strict';

function Luhn(number) {
  this.checkDigit = number % 10;
  this.addends = this.calculateAddends(number);
  this.checksum = this.calculateChecksum(this.addends);
  this.valid = this.determineIfValid(this.checksum);
}

Luhn.prototype = {
  calculateAddends: function(number) {

    var numberAsString = '' + number + '';
    var numbers = numberAsString.split('');
    var addends = [];

    for (var i = 0; i < numbers.length; i++) {
      var index = numbers.length - 1 - i;

      var currentAddend = parseInt(numbers[index], 10);

      if ((i + 1) % 2 === 0) {
        currentAddend = currentAddend * 2;
        if (currentAddend > 10) {
          currentAddend = currentAddend - 9;
        }
      }

      addends.push(currentAddend);
    }

    return addends.reverse();

  },
  calculateChecksum: function(numbers) {
    var sum = 0;
    for (var i = 0; i < numbers.length; i++) {
      sum += numbers[i];
    }

    return sum;
  },
  determineIfValid: function(sum) {
    return (sum % 10 === 0);
  }
};

Luhn.create = function(number) {
  var finalNumber = number * 10;
  var luhnNumber = new Luhn(finalNumber);
  var index = 0;

  while(!luhnNumber.valid) {
    finalNumber = number * 10 + index;
    luhnNumber = new Luhn(finalNumber);
    if (luhnNumber.valid) { break; }
    index += 1;
  }

  return finalNumber;
};

module.exports = Luhn;

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
trackler-2.0.0.1 tracks/javascript/exercises/luhn/example.js
trackler-2.0.0.0 tracks/javascript/exercises/luhn/example.js
trackler-1.0.4.1 tracks/javascript/exercises/luhn/example.js
trackler-1.0.4.0 tracks/javascript/exercises/luhn/example.js
trackler-1.0.3.0 tracks/javascript/exercises/luhn/example.js
trackler-1.0.2.1 tracks/javascript/exercises/luhn/example.js
trackler-1.0.2.0 tracks/javascript/exercises/luhn/example.js
trackler-1.0.1.2 tracks/javascript/exercises/luhn/example.js
trackler-1.0.1.1 tracks/javascript/exercises/luhn/example.js
trackler-1.0.1.0 tracks/javascript/exercises/luhn/example.js
trackler-1.0.0.1 tracks/javascript/exercises/luhn/example.js
trackler-1.0.0 tracks/javascript/exercises/luhn/example.js