Converting Roman Numerals to

Hello everyone, I wrote a program and I need the Roman numerals to be converted. In the code that I wrote, this issue was not resolved, I found a solution on the Internet, but still, the program does not pass tests using this method.

if (num === 0){
    return romanNumerals.join("")
}    
else if (num >= 1000){
    romanNumerals.push("M")
    num -= 1000
     return convertToRoman(num)
}
else if (num >= 900){
    romanNumerals.push("CM")
    num -= 900
    return convertToRoman(num)
}
else if (num >= 500){
    romanNumerals.push("D")
    num -= 500
    return convertToRoman(num)
}
else if (num >= 400){
    romanNumerals.push("CD")
    num -= 400
    return convertToRoman(num)
}
else if (num >= 100){
    romanNumerals.push("C")
    num -= 100
    return convertToRoman(num)
}
else if (num >= 90){
    romanNumerals.push("XC")
    num -= 90
    return convertToRoman(num)
}
else if (num >= 50){
    romanNumerals.push("L")
    num -= 50
    return convertToRoman(num)
}
else if (num >= 40){
    romanNumerals.push("XL")
    num -= 40
    return convertToRoman(num)
}
else if (num >= 10) { 
    romanNumerals.push("X")
    num -= 10
    return convertToRoman(num)
}
else if (num >=9){
    romanNumerals.push("IX")
    num -= 9
    return convertToRoman(num)
}
else if (num >=5) {
    romanNumerals.push("V")
    num -= 5
    return convertToRoman(num)
}
else if (num == 4){
    romanNumerals.push("IV")
    num -= 4
    return convertToRoman(num)

}
else if (num >=1){
    romanNumerals.push("I")
    num -=1
    return convertToRoman(num)
}
}

console.log(convertToRoman(900))
```Can you tell me how to fix this code so as not to break its functionality?
And another question, are there any services that do source code analysis and help with uniqueness? Thanks!

First, in your code you used else if. This means only 1 condition is executed per function call. So if I input 1001 it will output M instead of MI since it only passes through

num >= 1000

So the first thing you should do is change every else if to if and removing all return function except for num===0 because it terminates the function call.

Second, each function call only iterates through every condition once. It means if I input 2 then it outputs I instead of II.

Since you are using a approach that is taking the highest number available (greedy algorithm), you should put a while loop instead of if.
The code should end up something like this:

var romanNumerals = []; if (num === 0){ return romanNumerals.join("") } while (num >= 1000){ romanNumerals.push("M") num -= 1000 } while (num >= 900){ romanNumerals.push("CM") num -= 900 } while (num >= 500){ romanNumerals.push("D") num -= 500 } while (num >= 400){ romanNumerals.push("CD") num -= 400 } while (num >= 100){ romanNumerals.push("C") num -= 100 } while (num >= 90){ romanNumerals.push("XC") num -= 90 } while (num >= 50){ romanNumerals.push("L") num -= 50 } while (num >= 40){ romanNumerals.push("XL") num -= 40 } while (num >= 10) { romanNumerals.push("X") num -= 10 } while (num >=9){ romanNumerals.push("IX") num -= 9 } while (num >=5) { romanNumerals.push("V") num -= 5 } while (num == 4){ romanNumerals.push("IV") num -= 4 } while (num >=1){ romanNumerals.push("I") num -=1 }

Code Verified in Training on Roman Numerals Encoder | Codewars

1 Like

Yes, thanks for the advice, I fixed everything and the code works without a crash. Tell me, what if you use the AppRefactoring service to analyze the code, and then make it unique? Have you worked with him?