MCPcopy Create free account
hub / github.com/cryptii/cryptii / romanNumeralsToDecimal

Method romanNumeralsToDecimal

src/Encoder/NumeralSystem.js:247–276  ·  view source on GitHub ↗

* Translates given roman numerals to decimal. * @protected * @param {string} romanNumerals * @return {?number} Decimal or null, if not defined.

(romanNumerals)

Source from the content-addressed store, hash-verified

245 * @return {?number} Decimal or null, if not defined.
246 */
247 static romanNumeralsToDecimal (romanNumerals) {
248 romanNumerals = romanNumerals.toString().toUpperCase()
249
250 let index = 0
251 let decimal = 0
252 let error = false
253
254 let previousNumeral = 0
255 let numeral
256
257 while (!error && index < romanNumerals.length) {
258 // Find first roman numeral with the highest value
259 numeral = romanNumeralSymbols.findIndex(symbol =>
260 romanNumerals.substr(index, symbol.length) === symbol)
261
262 if (numeral !== -1 && numeral >= previousNumeral) {
263 // Append roman numeral
264 decimal += romanNumeralValues[numeral]
265 // Move cursor
266 index += romanNumeralSymbols[numeral].length
267 // Track previous numeral
268 previousNumeral = numeral
269 } else {
270 // Unexpected numeral
271 error = true
272 }
273 }
274
275 return !error ? decimal : null
276 }
277}

Callers 1

decodeNumberMethod · 0.80

Calls 3

toUpperCaseMethod · 0.80
toStringMethod · 0.80
substrMethod · 0.80

Tested by

no test coverage detected