I have written what appears to be a working (though possibly non-elegant!) solution to the challenge, but the test validator says the returns are wrong??
Logging the returns out to the console shows correct response - what am I missing here?
// build an object to hold temp values
let myObj = {
runTot: 0,
ret: ''
}
function convertToRoman(num) {
// asign value of num to object before we start
myObj.runTot = num;
// Numerals
const thousandsArr = ['','M','MM','MMM', 'MMMM'];
const hundredsArr = [ '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'];
const tensArr = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'];
const unitsArr = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
// general purpose function to modify the object
function decadeCalc(div,array){
if (myObj.runTot/div >=1){ // the running total / the decade unit is >= 1
let whole = Math.floor(myObj.runTot/div); // how many
myObj.ret += array[whole]; // add the correct numeral to the output
myObj.runTot-= whole*div; // what remains
}
}
// now call the function for each decade calc
decadeCalc(1000,thousandsArr); // thousands
decadeCalc(100,hundredsArr); // hundreds
decadeCalc(10,tensArr); // tens
decadeCalc(1,unitsArr); // unitss
return myObj.ret;
}
console.log(convertToRoman(16));
Ouput from above example : is XVI which is correct.
Help!
**Your browser information:**
User Agent is: Mozilla/5.0 (X11; CrOS aarch64 13816.34.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.51 Safari/537.36
.
Challenge: Roman Numeral Converter
Link to the challenge: