Integer to Roman Numeral Conversion

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will be exploring how to convert an integer to its roman numeral representation using JavaScript. We will use a lookup table to map the integer values to their corresponding roman numerals, and then use the reduce method to iterate over the values and build the roman numeral representation of the input integer. By the end of this lab, you will have a deeper understanding of how to manipulate arrays and strings in JavaScript to perform complex tasks.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") subgraph Lab Skills javascript/variables -.-> lab-28659{{"`Integer to Roman Numeral Conversion`"}} javascript/data_types -.-> lab-28659{{"`Integer to Roman Numeral Conversion`"}} javascript/arith_ops -.-> lab-28659{{"`Integer to Roman Numeral Conversion`"}} javascript/comp_ops -.-> lab-28659{{"`Integer to Roman Numeral Conversion`"}} javascript/higher_funcs -.-> lab-28659{{"`Integer to Roman Numeral Conversion`"}} end

Converting Integer to Roman Numeral

To convert an integer to its roman numeral representation, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.

  2. The function toRomanNumeral() accepts values between 1 and 3999 (both inclusive).

  3. Create a lookup table containing 2-value arrays in the form of (roman value, integer).

  4. Use Array.prototype.reduce() to loop over the values in lookup and repeatedly divide num by the value.

  5. Use String.prototype.repeat() to add the roman numeral representation to the accumulator.

Here is the code for the toRomanNumeral() function:

const toRomanNumeral = (num) => {
  const lookup = [
    ["M", 1000],
    ["CM", 900],
    ["D", 500],
    ["CD", 400],
    ["C", 100],
    ["XC", 90],
    ["L", 50],
    ["XL", 40],
    ["X", 10],
    ["IX", 9],
    ["V", 5],
    ["IV", 4],
    ["I", 1]
  ];
  return lookup.reduce((acc, [k, v]) => {
    acc += k.repeat(Math.floor(num / v));
    num = num % v;
    return acc;
  }, "");
};

You can test the function with these examples:

toRomanNumeral(3); // 'III'
toRomanNumeral(11); // 'XI'
toRomanNumeral(1998); // 'MCMXCVIII'

Summary

Congratulations! You have completed the Integer to Roman Numeral lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like