1
0
Fork 0

Roman numeral conversion added

This commit is contained in:
Joe L. Wroten 2015-11-02 11:05:20 -06:00
parent d1bc9711dd
commit 8ce0871e4e
3 changed files with 22 additions and 2 deletions

20
app.js
View file

@ -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('');
}
/**

View file

@ -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"
}
}

View file

@ -68,6 +68,7 @@
</div>
<script src="bower_components/vue/dist/vue.min.js"></script>
<script src="bower_components/lodash/lodash.min.js"></script>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>