Roman Numeral Converter-freecodecamp Algorithm Topic

Source: Internet
Author: User
Tags roman numeral converter

Roman numeral Converter

1. Requirements

    • Converts a given number into Roman numerals
    • All returned Roman numerals should be in uppercase form

2. Ideas

    • Array of corresponding Roman numerals for single-digit, 10-bit, hundred, thousand, respectively
    • Use Math.floor () to remove numbers from each bit of the number, and to find the corresponding Roman numerals in the previously defined array.

3. Code

function convert(num) {var arr1 = [‘‘,‘I‘,‘II‘,‘III‘,‘IV‘,‘V‘,‘VI‘,‘VII‘,‘VIII‘,‘IX‘];var arr10 = [‘‘,‘X‘,‘XX‘,‘XXX‘,‘XL‘,‘L‘,‘LX‘,‘LXX‘,‘LXXX‘,‘XC‘];var arr100 = [‘‘,‘C‘,‘CC‘,‘CCC‘,‘CD‘,‘D‘,‘DC‘,‘DCC‘,‘DCCC‘,‘CM‘];var arr1000 = [‘‘,‘M‘,‘MM‘,‘MMM‘];num = arr1000[Math.floor(num/1000)]+arr100[Math.floor(num%1000/100)]+arr10[Math.floor(num%100/10)]+arr1[Math.floor(num%10)];return num;}convert(36);

4. RELATED LINKS

    • Http://www.mathsisfun.com/roman-numerals.html
    • Http://www.runoob.com/jsref/jsref-floor.html

Roman Numeral Converter-freecodecamp Algorithm Topic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.