diff --git a/src/lib/typescript/functions/index.ts b/src/lib/typescript/functions/index.ts index 5eb856aad..57cde86bd 100644 --- a/src/lib/typescript/functions/index.ts +++ b/src/lib/typescript/functions/index.ts @@ -1,4 +1,4 @@ -export * from './num-length.function'; +export * from './integer-length.function'; export * from './num-start.function'; export * from './num-at.function'; export * from './lcm.function'; diff --git a/src/lib/typescript/functions/integer-length.function.spec.ts b/src/lib/typescript/functions/integer-length.function.spec.ts new file mode 100644 index 000000000..4d2296f51 --- /dev/null +++ b/src/lib/typescript/functions/integer-length.function.spec.ts @@ -0,0 +1,43 @@ +import { expect } from 'chai'; +import { integerLength, integerLengthMath } from './integer-length.function'; + +describe(`Integer length`, () => { + it(`should work with numbers larger than 1`, async () => { + expect(integerLength(123)).to.equal(3); + expect(integerLength(7653)).to.equal(4); + expect(integerLength(85476847)).to.equal(8); + }); + + it(`should work with 1`, async () => { + expect(integerLength(1)).to.equal(1); + }); + + it(`should work with 0`, async () => { + expect(integerLength(0)).to.equal(1); + }); + + it(`should work with negative numbers`, async () => { + expect(integerLength(-34312)).to.equal(5); + }); +}); + +// tslint:disable: deprecation +describe(`Integer length (Math)`, () => { + it(`should work with numbers larger than 1`, async () => { + expect(integerLengthMath(123)).to.equal(3); + expect(integerLengthMath(7653)).to.equal(4); + expect(integerLengthMath(85476847)).to.equal(8); + }); + + it(`should work with 1`, async () => { + expect(integerLengthMath(1)).to.equal(1); + }); + + it(`should work with 0`, async () => { + expect(integerLengthMath(0)).to.equal(0); + }); + + it(`should work with negative numbers`, async () => { + expect(integerLengthMath(-34312)).to.equal(5); + }); +}); diff --git a/src/lib/typescript/functions/num-length.function.ts b/src/lib/typescript/functions/integer-length.function.ts similarity index 99% rename from src/lib/typescript/functions/num-length.function.ts rename to src/lib/typescript/functions/integer-length.function.ts index 0a8fb2b95..9178e32e8 100644 --- a/src/lib/typescript/functions/num-length.function.ts +++ b/src/lib/typescript/functions/integer-length.function.ts @@ -2,7 +2,7 @@ * Returns the length of the integer part of a number. */ export function integerLength(n: number): number { - return Math.floor(n).toString().length; + return Math.floor(Math.abs(n)).toString().length; } /** diff --git a/src/lib/typescript/functions/num-at.function.ts b/src/lib/typescript/functions/num-at.function.ts index f0ed99018..e9879b393 100644 --- a/src/lib/typescript/functions/num-at.function.ts +++ b/src/lib/typescript/functions/num-at.function.ts @@ -6,7 +6,7 @@ * @param n the number of whichs length will be returned */ export function numAt(n: number, p: number): number { - return parseInt(n.toString()[p], 10); + return parseInt(n.toString()[p], 10) || 0; } /** diff --git a/src/lib/typescript/functions/num-length.function.spec.ts b/src/lib/typescript/functions/num-length.function.spec.ts deleted file mode 100644 index e3b966717..000000000 --- a/src/lib/typescript/functions/num-length.function.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { expect } from 'chai'; -import { numLength } from './num-length.function'; - -describe(`Number length`, () => { - it(`should work with numbers larger than 1`, async () => { - expect(numLength(123)).to.equal(3); - expect(numLength(7653)).to.equal(4); - expect(numLength(85476847)).to.equal(8); - }); - - it(`should work with 1`, async () => { - expect(numLength(1)).to.equal(1); - }); - - it(`should work with 0`, async () => { - expect(numLength(0)).to.equal(0); - }); - - it(`should work with negative numbers`, async () => { - expect(numLength(-34312)).to.equal(5); - }); -}); diff --git a/src/lib/typescript/intcode/intcode.class.ts b/src/lib/typescript/intcode/intcode.class.ts index efdf5be5c..dfdb2dba3 100644 --- a/src/lib/typescript/intcode/intcode.class.ts +++ b/src/lib/typescript/intcode/intcode.class.ts @@ -1,4 +1,4 @@ -import { numAt, numLength } from '@lib/functions'; +import { integerLength, numAt } from '@lib/functions'; import { Instruction, toInstruction } from './instruction.enum'; import { Mode } from './mode.enum'; @@ -130,7 +130,7 @@ export class IntCodeComputer implements Iterable { } private getMode(v: number, n: number): Mode | undefined { - return numAt(v, numLength(v) - n - 3); + return numAt(v, integerLength(v) - n - 3); } public *[Symbol.iterator](): IterableIterator {