From 8ce0871e4e957b19dcd744f6bdc428ea9a744d95 Mon Sep 17 00:00:00 2001 From: "Joe L. Wroten" Date: Mon, 2 Nov 2015 11:05:20 -0600 Subject: [PATCH] Roman numeral conversion added --- app.js | 20 +++++++++++++++++++- bower.json | 3 ++- index.html | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index e0d47a9..0d03661 100644 --- a/app.js +++ b/app.js @@ -53,11 +53,29 @@ var App = new Vue({ /** * Converts Integer to Roman Numerals + * Inspired by many stackOverflow roman numeral conversions * @param {[number]} n [whole number to be converted] * @return {[string]} [roman numeral conversion of provided whole number] */ function intToRoman(n) { - return n + 'TODO'; + // Set up mapping to compare crucial roman numerals to their integet values + var mapping = { + '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 + }, + // Placeholder result array. Could also just be a string we append too, but it feels nicer to work with an object and an array + result = []; + // Loop through each roman numeral + _.forEach(mapping, function(int, rom) { + // So long as the intToRoman (n) param is greater than the current symbol's matching int... + while(n >= int) { + // Add the current symbol to our results array + result.push(rom); + // Take the value of the current symbol's int and subtract it from 'n'. This may happen multiple times, such as "20" becoming "XX" + n -= int; + } + }); + return result.join(''); } /** diff --git a/bower.json b/bower.json index 1c0291e..235189e 100644 --- a/bower.json +++ b/bower.json @@ -19,6 +19,7 @@ "dependencies": { "vue": "~1.0.4", "bootstrap": "~3.3.5", - "bootstrap-material-design": "~0.3.0" + "bootstrap-material-design": "~0.3.0", + "lodash": "~3.10.1" } } diff --git a/index.html b/index.html index c569237..cde8a33 100644 --- a/index.html +++ b/index.html @@ -68,6 +68,7 @@ +