Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions roman_recursive.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
function to_roman (num) {
// your implementation code here
}
let hasil = '';
// let strnum = num.toString();
let angka = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
let romawi = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'];

if (num == 0){
return hasil
}

for ( let i = 0; i < romawi.length; i++){

if (num >= angka[i]){

hasil += romawi[i];
// console.log(hasil);
num -= angka[i];
return hasil + to_roman (num)
}
}
}

// Drive code
console.log('My totally sweet testing script for new roman\n')
console.log('input | expected | actual')
console.log('——————|——————————|———————')
console.log('4 | IV | ', to_roman(4))
console.log('4 | IV | ', to_roman(7))
console.log('9 | IX | ', to_roman(9))
console.log('13 | XIII | ', to_roman(13))
console.log('13 | XIII | ', to_roman(20))
console.log('1453 | MCDLIII | ', to_roman(1453))
console.log('1646 | MDCXLVI | ', to_roman(1646))