Skip to content

Synchronize Luhn exercise with canonical specs #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 28 additions & 51 deletions exercises/luhn/example.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,34 @@
export default class Luhn {

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

static calculateAddends(number) {
const numberAsString = '' + number + '',
numbers = [...numberAsString],
addends = [];

for (let i = 0; i < numbers.length; i++) {
const index = numbers.length - 1 - i;
let currentAddend = parseInt(numbers[index], 10);

if ((i + 1) % 2 === 0) {
currentAddend = currentAddend * 2;
if (currentAddend > 10) {
currentAddend = currentAddend - 9;
}
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;
}
addends.push(currentAddend);
}
return addends.reverse();
}

static calculateChecksum(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
}
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);

static determineIfValid(sum) {
return sum % 10 === 0;
}
return sum > 0 && sum % 10 === 0;
}

static create(number) {
let finalNumber = number * 10,
luhnNumber = new Luhn(finalNumber),
index = 0;
export default class Luhn {

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

}
57 changes: 16 additions & 41 deletions exercises/luhn/luhn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,34 @@ import Luhn from './luhn';

describe('Luhn',() => {

it('check digit',() => {
const luhn = new Luhn(34567);
expect(luhn.checkDigit).toEqual(7);
});

xit('check digit again',() => {
const luhn = new Luhn(91370);
expect(luhn.checkDigit).toEqual(0);
});

xit('addends',() => {
const luhn = new Luhn(12121);
expect(luhn.addends).toEqual([1, 4, 1, 4, 1]);
});

xit('too large added',() => {
const luhn = new Luhn(8631);
expect(luhn.addends).toEqual([7, 6, 6, 1]);
});

xit('checksum',() => {
const luhn = new Luhn(4913);
expect(luhn.checksum).toEqual(22);
});

xit('checksum again',() => {
const luhn = new Luhn(201773);
expect(luhn.checksum).toEqual(21);
it('single digit strings can not be valid', () => {
const luhn = new Luhn('1');
expect(luhn.valid).toEqual(false);
});

xit('invalid number',() => {
const luhn = new Luhn(738);
xit('A single zero is invalid', () => {
const luhn = new Luhn('0');
expect(luhn.valid).toEqual(false);
});

xit('invalid number',() => {
const luhn = new Luhn(8739567);
xit('valid Canadian SIN', () => {
const luhn = new Luhn('046 454 286');
expect(luhn.valid).toEqual(true);
});

xit('create valid number',() => {
const number = Luhn.create(123);
expect(number).toEqual(1230);
xit('invalid Canadian SIN', () => {
const luhn = new Luhn('046 454 287');
expect(luhn.valid).toEqual(false);
});

xit('create other valid number',() => {
const number = Luhn.create(873956);
expect(number).toEqual(8739567);
xit('invalid credit card', () => {
const luhn = new Luhn('8273 1232 7352 0569');
expect(luhn.valid).toEqual(false);
});

xit('create yet another valid number',() => {
const number = Luhn.create(837263756);
expect(number).toEqual(8372637564);
xit('valid strings with a non-digit added become invalid', () => {
const luhn = new Luhn('046a 454 286');
expect(luhn.valid).toEqual(false);
});

});