JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Tell us what’s happening: [ test of roman numeral converter not working
i think that got .

Your code so far

// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler

function convertToRoman(num) {

    let tabRomain = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"];    
    let arabicNumerals = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
    var romainNumber = "";
    var acc = 0;
    
    while (num !== 0 && 
        arabicNumerals.indexOf(num) === -1) {
        var FlorDIv = -1;
        if ((num % arabicNumerals[acc]) !== num) {
            florDiv = Math.floor((num / arabicNumerals[acc]));
            while (florDiv > 0) {
                romainNumber = romainNumber+tabRomain[acc];
                florDiv -= 1;
            }
            num = num % arabicNumerals[acc];
        }
        if (num>1&&num<4) {
            romainNumber = romainNumber.padEnd(romainNumber.length+num, "I");
            num = 0;
        }
        acc +=1;
    }
    if (arabicNumerals.indexOf(num) !== -1) {
        romainNumber += tabRomain[arabicNumerals.indexOf(num)];
    }
    
    return (romainNumber);
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Link to the challenge:

I got the right responses with "console.log (romainNumber)"in the place of “return (romainNumber)”.
could someone explain me why the return statement doesn’t work ? thank you.

JavaScript is case sensitive.
You have a var FlorDIv but you are using florDiv in the rest of the code.
Also, why use var here instead of let?

1 Like

ok thankyou.
I used a var instead of let because florDiv can change

Please read this article as I think you should use let instead of var.

< Difference between var and let in JavaScript - GeeksforGeeks>

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