JS Algorithms : Roman Numeral Converter

JS Algorithms : Roman Numeral Converter
0

#1

Tell us what’s happening:
Hi,

With the below code I passed the test. But when I checked the solution it looked quite different.

did I miss out some of the border cases in my code?

**My code **

 function convertToRoman(num) {
    const placeVal = [1,4,5,9,10,40,50,90,100,400,500,900,1000];
    const romNumeral = ['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M'];
    var str="";
    // checking value range
    if (num<1 || num>3999){
        console.log ("Not a valid number for conversion");
        return false;
    }
    while (num!==0){ 
        for (let j = 0; j<13; j++){
            while (num>=placeVal[j] && !(j<13 && num>=placeVal[j+1])){
                str+=romNumeral[j];
                num -= placeVal[j];
            }
        }
    }
    num = str;
    return num;
}

convertToRoman(36);

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter


#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Also, your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.

What do you mean by “when I checked the solution”? If you mean the Guide solution(s) for this challenge, then all I can say is everybody will come up with a different solution typically for this challenge. There are so many ways to solve it.


#3

thanks.
I posted a query for the first time, wasn’t aware of this.