Why is this roman numeral converter not giving the required output?

function convertToRoman(num) {
   // Create roman numeral to number lookup table
    const roman = {
        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
    }; // This is an Object
    // Create Roman Numeral Accumulator
    let accumulator = '';
    // Loop through lookup table
    for (const key in roman) {
        const numberValue = roman[key];
        console.log(key, numberValue);
        while (numberValue <= num) {
            num -= numberValue;
            accumulator += key;
    // while(currentNumber <= num) than subtract current number from num.Add symbol to accumulator.
    return accumulator;

Console Output:

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

On typing this code I got the above code which basically is showing all the values I had typed in roman (The Object). Although this code passes on freeCodeCamp, I still don’t understand why this is not giving me the symbol of the number 1000 but instead gives me a list of my Object. Please Explain.

Related Links:

I don’t understand the question. If I passes, it does return the correct value. You have chosen to add additional logging statements, but those logging statements don’t change the return value.

Okay, should what lines should I remove to get only the desired output? (Other than the lines given below)

Do you mean that you want to only log the output of the function call? Then you should add a log statement around the function call.

:point_up_2: You mean this right?

The return statement is for giving some value back to the program that made the function call.
The log statement is for writing stuff to the console for debugging or for keeping track of what is happening inside a function.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.