Hello,
I have been wrestling with this code for a while now and finally got it working (I thought). Although I doubt its the best solution I was quite happy to have figured it out. Please could someone explain to me why I am not passing the test cases using this?
let i = 0;
let convert = "";
function convertToRoman(num){
const roman = {
numerals: {
1000: "M",
900: "CM",
500: "D",
400: "CD",
100: "C",
90: "XC",
50: "L",
40: "XL",
10: "X",
9: "IX",
5: "V",
4: "IV",
3: "III",
2: "II",
1: "I"},
values: [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 3, 2 ,1]
}
// obfuscate function requirements from user
function converter(num, i) {
let numStore = num;
// validation condition 1 to run function
if(num <= 0 && convert.length == 0|| false || NaN ) {
return "Not a number in roman numerals"
}
// validation condition 2 to run function
else if(numStore >= 5000){
return "Please enter a smaller number"
}
//base-case
else if (i > roman.values.length || numStore == 0) {
return convert
}
//recursive condition 1
else if(numStore > roman.values[i]) {
convert = convert.concat(roman.numerals[roman.values[i]])
numStore -= roman.values[i]
return converter(numStore, i)
}
// recursive condition 2
else if(numStore == roman.values[i]) {
convert = convert.concat(roman.numerals[roman.values[i]])
numStore -= roman.values[i]
return converter(numStore, i)
}
// recursive condition 3
else if(numStore < roman.values[i]) {
return converter(numStore, i + 1)
}
}
// assign obsfucated function return value to variable
let converted = converter(num, i, convert)
// return converted value to user
return converted.toUpperCase()}
console.log(convertToRoman(36));
Any help is greatly appreciated. Thanks.