Changed the furthestFrom0() function + added tests

This commit is contained in:
Master-Guy 2022-03-18 09:30:25 +01:00
parent 90fd496a24
commit 1d07bf049f
2 changed files with 37 additions and 5 deletions

@ -203,12 +203,16 @@ class NumeralFormatter {
}
furthestFrom0(n1: number, n2 = 0, n3 = 0): number {
const minValue = Math.min(n1, n2, n3);
if(minValue < 0) {
return minValue;
} else {
return Math.max(n1, n2, n3);
if(isNaN(n1)) n1=0;
if(isNaN(n2)) n2=0;
if(isNaN(n3)) n3=0;
const furthestAbsolute = Math.max(Math.abs(n1), Math.abs(n2), Math.abs(n3));
switch(furthestAbsolute) {
case Math.abs(n1): return n1;
case Math.abs(n2): return n2;
case Math.abs(n3): return n3;
}
return 0;
}
parseMoney(s: string): number {

@ -218,3 +218,31 @@ describe('Numeral formatting of scientific text', () => {
//expect(numeralWrapper.parseMoney('-123.456n')).toBeCloseTo(-123456000000000000000000000000000);
});
});
describe('Finding the number furthest away from 0', () => {
test('should work if all numbers are equal', () => {
expect(numeralWrapper.furthestFrom0(0, 0, 0)).toEqual(0);
expect(numeralWrapper.furthestFrom0(1, 1, 1)).toEqual(1);
expect(numeralWrapper.furthestFrom0(123, 123, 123)).toEqual(123);
expect(numeralWrapper.furthestFrom0(-1, -1, -1)).toEqual(-1);
expect(numeralWrapper.furthestFrom0(-123, -123, -123)).toEqual(-123);
});
test('should work for different positive numbers, and for the largest number in each spot', () => {
expect(numeralWrapper.furthestFrom0(1, 2, 3)).toEqual(3);
expect(numeralWrapper.furthestFrom0(456, 789, 123)).toEqual(789);
expect(numeralWrapper.furthestFrom0(789123, 123456, 456789)).toEqual(789123);
});
test('should work for different negative numbers, and for the smallest number in each spot', () => {
expect(numeralWrapper.furthestFrom0(-1, -2, -3)).toEqual(-3);
expect(numeralWrapper.furthestFrom0(-456, -789, -123)).toEqual(-789);
expect(numeralWrapper.furthestFrom0(-789123, -123456, -456789)).toEqual(-789123);
});
test('should work for combined positive and negative numbers', () => {
expect(numeralWrapper.furthestFrom0(1, -2, 3)).toEqual(3);
expect(numeralWrapper.furthestFrom0(-456, 789, -123)).toEqual(789);
expect(numeralWrapper.furthestFrom0(789123, -123456, -456789)).toEqual(789123);
});
test('Should return 0 for invalid input', () => {
expect(numeralWrapper.furthestFrom0('abc', undefined, null)).toEqual(0);
});
});