Roman converter Js challenge

Hi comunity, hope you are doing well.

I’m struggling with the validation of the challenge. I’m checking with console.log (in line 73) that the variable that I’m returning is a string and it has the correct roman number constructed, but the validator is telling me that some validations are nos passing.

What could be happening???

My code is the following:

//let currentNum = 0
let str = []
let roman = ""

function convertToRoman(num) {
    
  let currentNum = num

  let value = 0
  let count = 0
  let divider = 0
  let down = 0
  let letter = num >= 1000 ? "M" : num >= 900 ? "CM" : num >= 500 ? "D" : num >= 400 ? "CD" : num >= 100 ? "C" : num >= 90 ? "XC" : num >= 50 ? "L" : num >= 40 ? "XL" : num >= 10 ? "X" : num >= 9 ? "IX" : num >= 5 ? "V" : num >= 4 ? "IV" : num > 0 ? "I" : "end"
  
  switch (letter){
    case "M" :
      value = 1000
      break
    case "CM" :
      value = 900
      break
    case "D" :
      value = 500
      break
    case "CD" :
      value = 400
      break
    case "C" :
      value = 100
      break
    case "XC" :
      value = 90
      break
    case "L" :
      value = 50
      break
    case "XL" :
      value = 40
      break
    case "X" :
      value = 10
      break
    case "IX" :
      value = 9
      break
    case "V" :
      value = 5
      break
    case "IV" :
      value = 4
      break
    case "I" :
      value = 1
      break
    case "end" :
      value = 0
      break
  }

  divider = currentNum / value
  count = Math.floor(divider)
  down = value * count
  currentNum -= down
  
  for (let i = count; i > 0 ; i--){
    str.push(letter)
  }
  roman = str.join("")
  
  if (currentNum > 0){
    convertToRoman(currentNum)
  } else {
    console.log(typeof(roman), roman) 
    str = []
    return roman
  }
}

convertToRoman(12)

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

You mean that I should define my variables inside the function? Because I tried the following and get the same result. Some validations are being passed, but others don’t.

// I declared the variables inside the function, like this:
function convertToRoman(num) {
let str =
let roman = “”
// …
}

// Intead of declaring them outside like this?

let str =
let roman = “”
function convertToRoman(num) {
// …
}

That is because you are making recursive calls, so you will have to modify your logic a little to be able to keep a recursive solution while not creating global variables.

You have a completely different problem when you put the variables inside your function definition. You are trying to cheat recursion by stashing the results of the recursive function calls in a global variable, but that breaks the reusabiltiy of your function.

You have a few options

  1. Use the recursive function call return values

  2. Stash the recursive state with some trick like default arguments

  3. Use a non-recursive version of your logic

Personally, I would do 3. The recursion isn’t really helping you here. Rarely is recursion the tool you want to use.

@RandellDawson @JeremyLT I began with a do {} while () logic but got into huge amount of loops and crashing the browser. That’s why I tried this approach.

I’ll try to replace the recursive with some loop in a different way. I’ll keep you posted

It worked!!! I got some of the logic into a second function and used a do {} while () calling it, some global variables to be accesible for both functions and solved the challenge,

Thank you both @JeremyLT @RandellDawson

Good job getting a passing solution!

Some people like making the problem go away by wrapping the function in yet another function.

I’m not really a a fan of that approach since it makes the code more complex than it needs to be.

1 Like

Thanks!!! I’ll get a coffe or a mate and start the third challenge :muscle:

1 Like