Write a recursive function that converts an integer into a string such that the number is represented in Roman Numerals in the most efficient way.
For example, the number 4 could be written as IIII but it's more efficient to use IVsince that's a shorter string.
Assume no number is greater than 4,000.
Here are the Roman Numeral equivalents you'll need to know:
- 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
function toRoman(num) {
// your code goes here
}
console.log(toRoman(128)); // should return "CXXVIII"
console.log(toRoman(2000)); // should return "MM"
console.log(toRoman(2017)); // should return "MMXVII"
console.log(toRoman(1999)); // should return "MCMXCIX"Given a an integer , return a string that represents the integer as a Roman Numeral..
What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?
- Input will always be an integer.
- Output should be a string.
- Most 'decoding' functions need some kind of object with key value pairs that correspond to the input and output.
- Will it be better to have numbers or letters as key, or vice versa?
- I want to ADD the letter to the string, and because the input is a number, the output will be interpolating the "keys" that are added to the string.
- Searching of Data
- Sorting of Data
- Pattern Recognition
- Build/Navigate a Grid
- Math
- Language API knowledge
- Optimization
- I believe I will be using an object, which means I will probably need to use either
Object.key()of afor - inloop. - The pros are that it will be creating a key to translate the number, but it will have to be iterated over repeatedly because of the size of the input.
- Create an "map" object for the roman numerals - similar to what is written in the prompt above - that has key: value pairs with the letter as the key and the corresponding number as it's value.
- Iterate over the object, and add the letter to a string, but only while the number is bigger than the value of the corresponding key
- Final subtract that number value from the input and iterate over the object again.
- Continue until the number is zero, then reture the string of letters.
https://replit.com/@DrewBradley/RomanNumerals#index.js
O(n)
