Roman Numeral Converter (Functional Program Help!)

I started learning javascript this March after so so doing very fine in HTML & CSS. I got my code to work. However, I need some tips on how I can make a functional program out of it.

function convertToRoman(num) {
  const numericValue = [ 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 ];
  const romanValue = [ "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" ];
  let iterator = numericValue.length - 1;
  let temp = 0;
  let romanNumeralValue = "";

  //make sure num is a number > 0
  if (typeof(num) === "number" && num > 0) {
    //do while num is > 0
    while(num > 0) {
      //get how many roman value to repeat, if 0 then below if does not apply
      temp = Math.floor(num / numericValue[iterator]);
      //go through each thousands, hundreds, tens and ones
      num = num % numericValue[iterator];
        
        //concatenate roman value from array into final romanNumeralValue string
      if (temp > 0) {
        for(let i = 1; i <= temp; i++) {
          romanNumeralValue += romanValue[iterator];
        }
      }
      // iterate down through the array, repeat the while loop until condition is met
      iterator--;
    }
  } else {
    romanNumeralValue = 0;
  }
  
  return romanNumeralValue;
}

console.log(convertToRoman(230));

What do you mean with functional program?

functional programming is a paradigm

Yep, although I’m not sure if that’s what OP means.

When someone says functional in terms of JS I assume they mean just some of the principles of the paradigm because JS is not a purely functional language

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.