Converting Numerals

Charles Butler Jr

--

For this week’s blog we’re looking at a common coding challenge that can be found on the likes of leetcode & freecodecamp i.e the roman numeral conversion calculator. With this calculator a user is able to give an integer [as the inpu]t & from there a method or series of methods will take that integer, separate the individual places & based on a set roman numeral logic will convert each part into an accurate roman numeral. Face-value this conversion logic isn’t too complicated but the initial problem I ran into was how would my program run and understand place values and if statements that can occur when dealing with the numbers 4/6, 9/11. All cases where the Roman Numeral “I” is placed before or after the V/X. But more on that later first since we are using Javascript for this example, we have to establish a key for our Roman Numerals & the best way to do that is to define an object and set the key / value pairs as our roman numerals and wrap all of this in a function called conToRom.

Next we’re going to have to loop through the object and for this we’ll need to use the “for..of” statement/loop. If you’re not familiar with the loop you can play with it a bit here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of . With this we’ll set the loop to go through each of our key / value pairs to match the input with the roman numeral & through “magic” (not really we’re going to use logic in a second) display the Roman Numeral that coincides.

Now it’s time for a while statement where we compare the number to the key / values and will remove the compared value & store the roman num into a separate array to display. Once it hits the value it matches with, it will add it to our answer array & then delete the value from the input integer. The algorithm will run this for each number in the input integer till finished and with a return statement prints out our conToRom conversion!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Charles Butler Jr
Charles Butler Jr

Written by Charles Butler Jr

Pre-Med student turned software engineer. Hoping to make code make sense & make sense out of all my code.

No responses yet

Write a response