Conversion de nombre romain en nombre arabe

This commit is contained in:
Divlo
2019-09-22 10:09:25 +02:00
parent 48a9989fd8
commit a2631210fe
6 changed files with 42 additions and 8 deletions

View File

@ -263,7 +263,7 @@ function binToUtf8(s){
}
// Convertis un nombre arabe en nombre romain
function convertRomanArabicNumbers(nombre) {
function convertArabicToRoman(nombre) {
// Tableau contenant chaque correspondance entre un nombre arabe et un nombre romain
const correspondances =
[
@ -313,9 +313,27 @@ function convertRomanArabicNumbers(nombre) {
return chiffresRomains;
}
// Convertis un nombre romain en nombre arabe
function convertRomanToArabic(str) {
var result = 0;
// the result is now a number, not a string
var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"];
for (var i = 0;i<=decimal.length;i++) {
while (str.indexOf(roman[i]) === 0){
//checking for the first characters in the string
result += decimal[i];
//adding the decimal value to our result counter
str = str.replace(roman[i],'');
//remove the matched Roman letter from the beginning
}
}
return result;
}
// Vérifie si un nombre fait partie des nombres d'Armstrong ou non
function armstrongNumber(nombre) {
let nombreString = nombre.toString();
function armstrongNumber(number) {
let nombreString = number.toString();
let nombreStringLength = nombreString.length;
let result = 0;

View File

@ -212,14 +212,23 @@ $(function () {
$("#submitConvertRomanArabicNumbers").click(function()
{
let numbersValue = $('#numbersArabic').val();
let convertNumberType = $("#convertNumberType option:selected").text();
if(isEmptyValue(numbersValue)) {
$('.results').html(emptyMessageError);
}
else if (!isNaN(parseInt(numbersValue))) {
let result = convertRomanArabicNumbers(parseFloat(numbersValue.replace(/\s/g,'')));
else if (!isNaN(parseInt(numbersValue)) && convertNumberType === "Nombre Romain") {
let result = convertArabicToRoman(parseInt(numbersValue.replace(/\s/g,'')));
$('.results').html(`<b>${formatNumberResult(numbersValue.replace(/\s/g,''))}</b> s'écrit <b>${result}</b> en chiffres romains.`);
}
else if (convertNumberType === "Nombre Arabe") {
if (!isNaN(parseInt(numbersValue))) {
$('.results').html(`<b>${numbersValue}</b> est déjà en chiffres arabes.`);
} else {
let result = convertRomanToArabic(numbersValue);
$('.results').html(`<b>${numbersValue}</b> s'écrit <b>${result}</b> en chiffres arabes.`);
}
}
else {
$('.results').html(messageError);
}