Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
trying to convert decimal numbers to roman numbers with for in javascript

I don't know if I can ask or not but the answers that appear here don't help me. I'm trying to convert decimal numbers to roman numbers in javascript. I'm dividing the numbers in digits and seeing the different cases. Until now, I'm in 2 digits case. The problem is that when I try to read the "decenas" (tens) part, it says that the number I'm looking for doesn't exist in the object, and that's not correct. Can you help me figure out why? When I pass 12, it only returns the unit part.

    function convertToRoman(num) {
     let nums = num.toString();
     let roman = ''; //here I'll keep the results
     let numbers = {
       '1': 'I',
       '2': 'II',
       '3': 'III',
       '4': 'IV',
       '5': 'V',
       '6': 'VI',
       '7': 'VII',
       '8': 'VIII',
       '9': 'IX',
       '10': 'X'
     };
     let unidad = nums[1]; //here I keep the units part
     let decena =nums[0]*10; //here the tens part
     console.log("nums Length:",nums.length);
     //If the number has 1 digit:
     if (nums.length === 1){
      for (let n in numbers){
        if (n === nums){
          roman = numbers[n];
        }
      }
     }
     //If the number has 2 digits:
     if (nums.length === 2){
       for ( let n in numbers){
         if(n === decena){
           roman += numbers[n];
         }
       }
       for ( let n in numbers){
         if(n === unidad){
          roman += numbers[n];
         }
       }
     }
    
    
    console.log("decena:", decena, "unidad:", unidad, "En romano:", roman);
     
     return roman
    }
    
    convertToRoman(12);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use this code for converting -

function convertToRoman(num) 
{
  let map = {
    1000 : "M",
    900 : "CM",
    500 : "D",
    400 : "CD",
    100 : "C",
    90: "XC",
    50: "L",
    40: "XL",
    10: "X",
    9: "IX",
    5: "V",
    4: "IV",
    1: "I"
  }
  let roman = "";
  let romankeys = Object.keys(map).reverse();
  romankeys.forEach((keys) => {
      while(keys <= num)
      {
        roman += map[keys];
        num -= keys;
      }
        
  });
  return roman;
}
console.log(convertToRoman(58));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda