From d21b8a90b2545680e01a60a704124cf8f5064b23 Mon Sep 17 00:00:00 2001 From: Zuhri Nurhuda Date: Mon, 16 Oct 2017 18:30:48 +0700 Subject: [PATCH] roman numerals done --- roman_numerals.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/roman_numerals.js b/roman_numerals.js index 8019de3..28383d4 100644 --- a/roman_numerals.js +++ b/roman_numerals.js @@ -1,5 +1,29 @@ function to_roman (num) { // your implementation code here + let output = '' + let roman = { + M: 1000, + CM: 900, + D: 500, + CD: 400, + C: 100, + XC: 90, + L: 50, + XL: 40, + X: 10, + IX: 9, + V: 5, + IV: 4, + I: 1 + } + + for (let key in roman) { + while (num >= roman[key]) { + output += key + num -= roman[key] + } + } + return output } // Drive code