Write a 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 IV
since 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"
- Copy this markdown and paste in your own, privte gist
- In your private gist, fill out the questions below
- Submit by the due time as instructed in Zoom
Do not publish your code on a public repl.it or repo or other public means.
Convert numbers into a string of roman numerals.
What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?
Any prototype method is valid to use, should be dynamic.
There needs to be some algorithmic way of converting without a long if/else statement. Could probably be converted to an array and reversed before being iterated through to covert each number. Could also create a helper function that takes in the pertinent letters and change them based on the place the number is in, combine using a reduce statement?
- Searching of Data
- Sorting of Data
- Pattern Recognition
- Build/Navigate a Grid
- Math
- Language API knowledge
- Optimization
Array
make helper function to determine structure of roman numerals for that number, with argument number that pertains to the letters being used (and number being converted), return a string create variable of number parameter being split and reversed iterate through new variable, passing in index to helper function and number being converted return conversion string