Skip to content

Commit b538ed8

Browse files
committed
Created constants for errors
1 parent 6507a2b commit b538ed8

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

day-02-dive.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const UNNEXPECTED_COMMAND_ERROR = new Error('Received unexpected command');
2+
13
export const enum Commands {
24
forward = 'forward',
35
up = 'up',
@@ -59,7 +61,7 @@ export default class DiverCommandsInterpreter {
5961
break;
6062

6163
default:
62-
throw new Error('Received unexpected command: ' + this.commands[i][0]);
64+
throw UNNEXPECTED_COMMAND_ERROR;
6365
}
6466
}
6567
return [offsetX, offsetY];
@@ -106,7 +108,7 @@ export default class DiverCommandsInterpreter {
106108
break;
107109

108110
default:
109-
throw new Error('Received unexpected command: ' + this.commands[i][0]);
111+
throw UNNEXPECTED_COMMAND_ERROR;
110112
}
111113
}
112114
return [offsetX, offsetY];

day-03-binary-diagnostic.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export default class DiagnosticReportCalculator {
1+
export const EMPTY_READINGS_ERROR = new Error('Readings were empty');
2+
export const UNCERTAIN_OUTCOME_ERROR = new Error('Unable to determine outcome because the number of 0 bits are equal to 1 bits');
3+
export const INVALID_BINARY_CHARACTER_ERROR = new Error('Invalid binary character. Exptected 0 or 1');
4+
export const COULD_NOT_DETERMINE_OXYGEN_RATING_ERROR = new Error('Could not determine oxygen rating');
5+
export const COULD_NOT_DETERMINE_CO2_SCRUBBER_RATING_ERROR = new Error('Could not determines CO2 Scrubber rating');
6+
7+
export class DiagnosticReportCalculator {
28
private binaryNumbers: string[] = [];
39

410
constructor(binaryNumbers: string[]) {
@@ -40,7 +46,7 @@ export default class DiagnosticReportCalculator {
4046
*/
4147
computeEpsilonAndGamma(): [gamma: number, epsilon: number] {
4248
if (this.binaryNumbers.length <= 0) {
43-
throw new Error('Readings were empty');
49+
throw EMPTY_READINGS_ERROR;
4450
}
4551
const inputLength = this.binaryNumbers[0].length;
4652
let gammaBinary = '';
@@ -56,7 +62,7 @@ export default class DiagnosticReportCalculator {
5662
}
5763
}
5864
if (zeroesCount == onesCount) {
59-
throw new Error('Unable to determine outcome because the number of 0 bits are equal to 1 bits for position ' + i);
65+
throw UNCERTAIN_OUTCOME_ERROR;
6066
}
6167
gammaBinary += zeroesCount > onesCount ? '0' : '1';
6268
epsilonBinary += zeroesCount > onesCount ? '1' : '0';
@@ -111,7 +117,7 @@ export default class DiagnosticReportCalculator {
111117
numbersWithOne.push(binaryNumber);
112118
break;
113119
default:
114-
throw new Error(binaryNumber[digitPositionToAnalyze] + ' is not a valid binary character. Exptected 0 or 1.');
120+
throw INVALID_BINARY_CHARACTER_ERROR;
115121
}
116122
}
117123
let keepNumbersWith: '0' | '1';
@@ -136,7 +142,7 @@ export default class DiagnosticReportCalculator {
136142
const currentLength = oxygenBinaryNumbers.length;
137143
oxygenBinaryNumbers = this.filterForRating(oxygenBinaryNumbers, i, true, '1');
138144
if (oxygenBinaryNumbers.length == currentLength) {
139-
throw new Error('Could not determines oxygen rating.');
145+
throw COULD_NOT_DETERMINE_OXYGEN_RATING_ERROR;
140146
}
141147
}
142148

@@ -145,7 +151,7 @@ export default class DiagnosticReportCalculator {
145151
const currentLength = co2ScrubberBinaryNumbers.length;
146152
co2ScrubberBinaryNumbers = this.filterForRating(co2ScrubberBinaryNumbers, i, false, '0');
147153
if (co2ScrubberBinaryNumbers.length == currentLength) {
148-
throw new Error('Could not determines CO2 Scrubber rating.');
154+
throw COULD_NOT_DETERMINE_CO2_SCRUBBER_RATING_ERROR;
149155
}
150156
}
151157

0 commit comments

Comments
 (0)