Skip to content

Instantly share code, notes, and snippets.

@outsideris
Created July 2, 2012 00:16
Show Gist options
  • Save outsideris/3030160 to your computer and use it in GitHub Desktop.
Save outsideris/3030160 to your computer and use it in GitHub Desktop.
Luhn algorithm
module.exports = {
stepOne: function(cardNo) {
return cardNo
.split('')
.reverse()
.map(function(v, i) {
if (i%2 === 1) {
return v * 2;
} else {
return v;
}
})
.reverse()
.join('');
}
, stepTwo: function(value) {
var sum = 0;
value.split('')
.forEach(function(e) {
sum += e * 1;
});
return sum;
}
, stepThree: function(value) {
var baseNum = Math.ceil((value + 1) / 10) * 10;
return baseNum - value;
}
}
var should = require('should')
, luhn = require('../src/luhn');
describe('LUHN 알고리즘', function() {
var CARD_NO = '5433330382087507';
it('㉠카드 번호의 우측 숫자부터 매 2번째 숫자마다 2를 곱한다', function() {
//console.log(luhn.stepOne(CARD_NO));
luhn.stepOne(CARD_NO).should.eql('1046363031620814507');
});
it('㉡위 ㉠에서 2를 곱하지 않은 숫자들과 ㉠에서 나온 숫자들을 합한다', function() {
luhn.stepTwo('1046363031620814507').should.eql(60);
});
it('㉢위 ㉡에서 나온 수와 그 다음으로 큰 10단위 수와의 차이를 구한다', function() {
luhn.stepThree(60).should.eql(10);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment