I was working on transforming numerals to Roman numbers. With a little a bit of work I got my code to work, but for some reason, I feel it is off.
Here is the code below. Run the code snippet. You will see the result will display in the console.
Basically after the for and the if statement start, you will see the following.
cardinal = roman[i].key;
converter = roman[i].romano;
converted.push(converter);
array = num.toString().split('');
array.splice(0,1);
rejoin = array.join('');
num = Number(rejoin);
after num=Number(rejoin); I wanted to have the code go back to the beginning and repeat until num=0. But I cannot seem to get it work. it is like the loop or the if the statement is broken. The new value of num does not carry back at the very beginning of the loop.
So I had to refactor it a bit and add a function cardinal that just repeats the initial code and add a do while. though it works I am still puzzled why the num does not carry back at the beginning. Any explanation will be appreciated.
Explanation of the work on num
Let's say you enter a value a 450. As the 450 goes through the code, the code checks the closest reference to 450. It will check in the object and find 400 and convert it to Roman. Next, I want to drop the number 4 of 450 and stay with the 50 and I want the 50 to be referenced in the object and converted. I want to do it until the last number is 0.
Thanks for your feedbacks.
function convertToRoman(num) {
var cardinal ;
var array = [];
var converter =[];
var converted=[];
var rejoin=[];
var roman = [
{"key": 1, "romano":'I'},
{"key": 2, "romano":'II'},
{"key": 3, "romano":'III'},
{"key": 4, "romano":'IV'},
{"key": 5, "romano":'V'},
{"key": 6, "romano":'VI'},
{"key": 7, "romano":'VII'},
{"key": 8, "romano":'VIII'},
{"key": 9, "romano":'IX'},
{"key": 10, "romano":'X'},
{"key": 20, "romano":'XX'},
{"key": 30, "romano":'XXX'},
{"key": 40, "romano":'XL'},
{"key": 50, "romano":'L'},
{"key": 60, "romano":'LX'},
{"key": 70, "romano":'LXX'},
{"key": 80, "romano":'LXXX'},
{"key": 90, "romano":'XC'},
{"key": 100, "romano":'C'},
{"key": 200, "romano":'CC'},
{"key": 300, "romano":'CCC'},
{"key": 400, "romano":'CD'},
{"key": 500, "romano":'D'},
{"key": 600, "romano":'DC'},
{"key": 700, "romano":'DCC'},
{"key": 800, "romano":'DCCC'},
{"key": 900, "romano":'CM'},
{"key": 1000, "romano":'M'},
{"key": 2000, "romano":'MM'},
{"key": 3000, "romano":'MMM'},
{"key": 4000, "romano":'MV'},
{"key": 5000, "romano":'|V'}
];
function cardinals (num) {
for (i=0 ; i< roman.length; i++) {
if ( (num > roman[i].key && num < roman[i+1].key) || (num == roman[i].key) ) {
cardinal = roman[i].key;
converter = roman[i].romano;
converted.push(converter);
}
}
}
for (i=0 ; i< roman.length; i++) {
if ( (num > roman[i].key && num < roman[i+1].key) || (num == roman[i].key) ) {
cardinal = roman[i].key;
converter = roman[i].romano;
converted.push(converter);
array = num.toString().split('');
array.splice(0,1);
rejoin = array.join('');
num = Number(rejoin);
cardinals(num);
do {
array = num.toString().split('');
array.splice(0,1);
rejoin = array.join('');
num = Number(rejoin);
cardinals(num);
} while (num!==0);
}
}
converted = converted.join('');
return converted;
}
console.log(convertToRoman(3999));
Aucun commentaire:
Enregistrer un commentaire