Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
convertir numeros romanos usando for loop

Parece que no puedo encontrar lo que me estoy perdiendo? si pruebo 3 me devolverá II en lugar de III lo mismo si es 8 me devolverá VII en lugar de VIII. Acabo de agregar el 8 y el 3 en mi variable de números

 VIII: 8, V: 5, IV: 4, III: 3, I: 1,

pero si tienen alguna sugerencia para arreglarlo sin agregarlo en los números, sería genial

 function convertToRoman(num) { //values of the numbers let numerals = { 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, }; //passing the new values of the new numbers to convert into numerals let newNumeral = ""; //checking the values of numerals objects for (let i in numerals) { //using the j variable of num, if the num is still greater than the numerals index it will increment the key to the variable newNumerals and stop the loop, it will subtract the num to the values of numerals . for (let j= 0; j <= num; j++ ){ if(num >= numerals[i]) { newNumeral += i; num -= numerals[i]; } } } //return to newNumerals to see the new value. return newNumeral; } console.log(convertToRoman(3));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El segundo ciclo for es un poco raro, no usas j para nada.

Puede reemplazar eso y el if dentro con un ciclo while más simple.

 //values of the numbers let numerals = { 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, }; const convertToRoman = (num) => { let newNumeral = ""; for (let i in numerals) { while (num >= numerals[i]) { newNumeral += i; num -= numerals[i]; } } return newNumeral; } console.log(convertToRoman(3)); console.log(convertToRoman(1990));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!