Last active
August 29, 2015 14:10
-
-
Save hwclass/c155a0b8ae472679d9d7 to your computer and use it in GitHub Desktop.
Some variations to change a number value into Roman type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var romanValues = new Array(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1); | |
var romanStrings = new Array("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"); | |
function calculate(hinduArabicNumber) { | |
var numb = parseInt(hinduArabicNumber); | |
if (isNaN(numb)) { | |
alert("Please enter a number"); | |
return; | |
} | |
if ( numb > 9999 || numb < 1) { | |
alert("Please enter a number between 1 and 9999"); | |
return; | |
} | |
var stringFormatOfTheGivenNumberInRoman = ''; | |
var tempNumb = numb; | |
while (tempNumb > 0) { | |
var i; | |
for (i=0; i < romanValues.length; i++) { | |
if ( tempNumb >= romanValues[i] ) { | |
stringFormatOfTheGivenNumberInRoman += romanStrings[i]; | |
tempNumb -= romanValues[i]; | |
break; | |
} | |
} | |
} | |
return stringFormatOfTheGivenNumberInRoman; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var romanValues = new Array(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1); | |
var romanStrings = new Array("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"); | |
function calculate(hinduArabicNumber) { | |
var numb = parseInt(hinduArabicNumber); | |
if (!isNaN(numb)) { | |
if ( numb <= 9999 || numb > 1) { | |
var stringFormatOfTheGivenNumberInRoman = ''; | |
var tempNumb = numb; | |
while (tempNumb > 0) { | |
var i; | |
for (i=0; i < romanValues.length; i++) { | |
if ( tempNumb >= romanValues[i] ) { | |
stringFormatOfTheGivenNumberInRoman += romanStrings[i]; | |
tempNumb -= romanValues[i]; | |
break; | |
} | |
} | |
} | |
return stringFormatOfTheGivenNumberInRoman; | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RomanAlphabet = function () { | |
this.romanValues = new Array(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1); | |
this.romanStrings = new Array("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"); | |
this.convertToRomanNumber = function (hinduArabicNumber) { | |
var self = this; | |
var numb = parseInt(hinduArabicNumber); | |
if (!isNaN(numb)) { | |
if ( numb <= 9999 || numb > 1) { | |
var stringFormatOfTheGivenNumberInRoman = ''; | |
var tempNumb = numb; | |
while (tempNumb > 0) { | |
var i; | |
for (i=0; i < self.romanValues.length; i++) { | |
if ( tempNumb >= self.romanValues[i] ) { | |
stringFormatOfTheGivenNumberInRoman += romanStrings[i]; | |
tempNumb -= romanValues[i]; | |
console.log(stringFormatOfTheGivenNumberInRoman); | |
break; | |
} | |
} | |
} | |
return stringFormatOfTheGivenNumberInRoman; | |
} | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Number.prototype.convertToRomanNumber = function () { | |
var romanValues = new Array(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1); | |
var romanStrings = new Array("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"); | |
var numb = parseInt(this); | |
if (!isNaN(numb)) { | |
if ( numb <= 9999 || numb > 1) { | |
var stringFormatOfTheGivenNumberInRoman = ''; | |
var tempNumb = numb; | |
while (tempNumb > 0) { | |
var i; | |
for (i=0; i < romanValues.length; i++) { | |
if ( tempNumb >= romanValues[i] ) { | |
stringFormatOfTheGivenNumberInRoman += romanStrings[i]; | |
tempNumb -= romanValues[i]; | |
break; | |
} | |
} | |
} | |
return stringFormatOfTheGivenNumberInRoman; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment