Skip to content

Commit

Permalink
Merge pull request #19011 from calixteman/roman
Browse files Browse the repository at this point in the history
Simplify toRomanNumerals function
  • Loading branch information
calixteman authored Nov 6, 2024
2 parents b666c4f + d59f964 commit f520d23
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,13 @@ function toRomanNumerals(number, lowerCase = false) {
Number.isInteger(number) && number > 0,
"The number should be a positive integer."
);
const romanBuf = [];
// Thousands
while (number >= 1000) {
number -= 1000;
romanBuf.push("M");
}
// Hundreds
let pos = (number / 100) | 0;
number %= 100;
romanBuf.push(ROMAN_NUMBER_MAP[pos]);
// Tens
pos = (number / 10) | 0;
number %= 10;
romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
// Ones
romanBuf.push(ROMAN_NUMBER_MAP[20 + number]); // eslint-disable-line unicorn/no-array-push-push

const romanStr = romanBuf.join("");
return lowerCase ? romanStr.toLowerCase() : romanStr;

const roman =
"M".repeat((number / 1000) | 0) +
ROMAN_NUMBER_MAP[((number % 1000) / 100) | 0] +
ROMAN_NUMBER_MAP[10 + (((number % 100) / 10) | 0)] +
ROMAN_NUMBER_MAP[20 + (number % 10)];
return lowerCase ? roman.toLowerCase() : roman;
}

// Calculate the base 2 logarithm of the number `x`. This differs from the
Expand Down

0 comments on commit f520d23

Please sign in to comment.