I have reached an impasse with my code so I took a peek at the Basic Solution to see how far off I was. The most glaring difference was that the Basic Solution uses 2 separate arrays (of decimals and their corresponding roman numerals) and my solution is going to use an object:
var numConvertor = {1000:'M', 900:'CM', 500:'D', 400:'CD', 100:'C', 90:'XC', 50:'L', 40:'XL', 10:'X', 9:'IX', 5:'V', 4:'IV', 1: 'I'};
I am running into many issues which essentially require me to convert to arrays. That is, at some point, I will have to have an array of the properties (of my object, numConvertor
) that I can compare against. As per this Q&A, I can do that but then I will end up with an array of strings not numbers. Which seems like I am not only moving towards the Basic Solution approach (i.e. arrays) but moving even further backwards b/c I end up with an array of string which I then have to convert to numbers in order to compare and convert…?
Is there any point in trying to salvage a solution that uses object instead of arrays?
And if so, is what I outlined above the correct course of action?
Thank you!
Your code so far
function convertToRoman(num) {
//create object of decimal to roman key/values
var numConvertor = {1000:'M', 900:'CM', 500:'D', 400:'CD', 100:'C', 90:'XC', 50:'L', 40:'XL', 10:'X', 9:'IX', 5:'V', 4:'IV', 1: 'I'};
//break decimal number into array of units
function numBreakdown(n) {
if (n == 0) return [0];
var arr = [];
var i = 1;
while (n > 0) {
arr.unshift((n % 10) * i);
n = Math.floor(n / 10);
i *= 10;
};
return arr;
}
let arrNumBreakdown = numBreakdown(num);
//go through array and convert each to a roman numeral based on numConvertor object
for(var i = 0; i < arrNumBreakdown.length; i++){
//is there a way to check value of property to compare which is the largest number that is smaller than numBreakdown[i]?
//Or do I HAVE to use the array methodology as per Basic Solution?
}
//combine results from each iteration into one string value?
}
//convertToRoman(36);
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter/