As a newcomer to the world of coding, I have taken on the challenge of mastering the Roman Numeral Kata using Javascript. I am pleased to report that all the specifications are passing successfully. After refactoring the spec file, I am now focusing on refactoring the main file.
This is what the code used to look like:
function roman(number) {
var conversion = ''
if (number == 100){
conversion += 'C'
number -= 100
}
// omitted code
while (number >= 1){
conversion += 'I'
number -= 1
}
return conversion
}
After refactoring, the code looks like this:
function roman(number) {
var denominations = {
100: 'C',
// omitted code
5: 'V',
4: 'IV',
1: 'I'
}
var conversions = ''
_.each(denominations, function(roman_num, natural_num) {
while (number >= natural_num){
conversions = conversions + roman_num
number -= natural_num
}
})
return conversions
}
I have been troubleshooting using the JS console in Chrome and it seems that instead of iterating through each denomination, the code keeps getting stuck at 1.
Furthermore, since I am using Jasmine, the error messages I see are as follows:
Expected 'IIII' to equal 'IV'
Expected 'IIIII' to equal 'V'
And so forth.
My current questions are: 1. Why does the code only return values for 1, 'I', and how can I resolve this issue? 2. What steps should I take to fix this particular problem?
Thank you in advance!