Tell us what’s happening:
Hey guys, so I tried solving this problem with a switch case, but then realized later, that I can only pass a value to a switch case and if conditions in those cases don’t work.
Is there a way to modify this switch case or should I use a different approach?
Tried not to use more than ten if statements in my code since it would be bad practice…
Your code so far
function convertToRoman(num) {
const roman = "";
const helper = (factor, letter, num) => {
let x = Math.floor(num / factor)
for (let i = 0; i<x; i++){
roman.concat(""+letter)
}
num = num-x*factor
roman.concat(convertToRoman(num))
}
switch(num){
case num > 1000:
helper(1000, "M", num)
break
case num > 900:
helper(900, "CM", num)
break
case num > 500:
helper(500, "D", num)
break
case num > 400:
helper(400, "CD", num)
break
case num > 100:
helper(100, "C", num)
break
case num > 90:
helper(90, "XC", num)
break
case num > 50:
helper(50, "L", num)
break
case num > 40:
helper(40, "XL", num)
break
case num > 10:
helper(10, "X", num)
break
case num > 9:
helper(9, "IX", num)
break
case num > 5:
helper(5, "V", num)
break
case num > 4:
helper (4, "IV", num)
break
case num > 1:
helper (1, "I", num)
break
default:
console.log("Something went wrong")
}
return roman;
}
console.log(convertToRoman(36));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41
Challenge: JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter
Link to the challenge: