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 {
2
8
private binaryNumbers : string [ ] = [ ] ;
3
9
4
10
constructor ( binaryNumbers : string [ ] ) {
@@ -40,7 +46,7 @@ export default class DiagnosticReportCalculator {
40
46
*/
41
47
computeEpsilonAndGamma ( ) : [ gamma : number , epsilon : number ] {
42
48
if ( this . binaryNumbers . length <= 0 ) {
43
- throw new Error ( 'Readings were empty' ) ;
49
+ throw EMPTY_READINGS_ERROR ;
44
50
}
45
51
const inputLength = this . binaryNumbers [ 0 ] . length ;
46
52
let gammaBinary = '' ;
@@ -56,7 +62,7 @@ export default class DiagnosticReportCalculator {
56
62
}
57
63
}
58
64
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 ;
60
66
}
61
67
gammaBinary += zeroesCount > onesCount ? '0' : '1' ;
62
68
epsilonBinary += zeroesCount > onesCount ? '1' : '0' ;
@@ -111,7 +117,7 @@ export default class DiagnosticReportCalculator {
111
117
numbersWithOne . push ( binaryNumber ) ;
112
118
break ;
113
119
default :
114
- throw new Error ( binaryNumber [ digitPositionToAnalyze ] + ' is not a valid binary character. Exptected 0 or 1.' ) ;
120
+ throw INVALID_BINARY_CHARACTER_ERROR ;
115
121
}
116
122
}
117
123
let keepNumbersWith : '0' | '1' ;
@@ -136,7 +142,7 @@ export default class DiagnosticReportCalculator {
136
142
const currentLength = oxygenBinaryNumbers . length ;
137
143
oxygenBinaryNumbers = this . filterForRating ( oxygenBinaryNumbers , i , true , '1' ) ;
138
144
if ( oxygenBinaryNumbers . length == currentLength ) {
139
- throw new Error ( 'Could not determines oxygen rating.' ) ;
145
+ throw COULD_NOT_DETERMINE_OXYGEN_RATING_ERROR ;
140
146
}
141
147
}
142
148
@@ -145,7 +151,7 @@ export default class DiagnosticReportCalculator {
145
151
const currentLength = co2ScrubberBinaryNumbers . length ;
146
152
co2ScrubberBinaryNumbers = this . filterForRating ( co2ScrubberBinaryNumbers , i , false , '0' ) ;
147
153
if ( co2ScrubberBinaryNumbers . length == currentLength ) {
148
- throw new Error ( 'Could not determines CO2 Scrubber rating.' ) ;
154
+ throw COULD_NOT_DETERMINE_CO2_SCRUBBER_RATING_ERROR ;
149
155
}
150
156
}
151
157
0 commit comments