Roman Numeral Converter question about my while loop

quick question this while loop.
while(myNum[myNum.length-1] >5 && myNum[myNum.length-1] <9){
myNum[myNum.length-1] = numToRoman[5]} why is it returning undefined?

  **Your code so far**
function convertToRoman(num) {
let myArr = [];
let myNum = num.toString().split("").map(Number)
let numToRoman ={1:"I", 2:"II", 3:"III", 4:"IV",5:"V",9:"IX", 10:"X",40:"XL",50:"L",90:"XC",100:"C",400:"CD",500:"D",900:"CM", 1000:"M"}

for(let i=0; i<myNum.length; i++){
if(myNum.length === 2){
  return numToRoman[10] +numToRoman[myNum[1]]
}
}
while(myNum[myNum.length-1] >5 && myNum[myNum.length-1] <9){
  myNum[myNum.length-1] = numToRoman[5]}

}


console.log(convertToRoman(17));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Roman Numeral Converter

Link to the challenge:

For the example call above, your code never makes it to the while loop. It is the following return statement inside your for loop that is causing the Xundefined return value:

return numToRoman[10] + numToRoman[myNum[1]]

Why? Because in this case myNum[1] is equal to 7 and numToRoman[7] evaluates to undefined because there is no property named 7 in the object. Also, I am not sure why you are even using a for loop to surround this if statement. It serves no purpose.

1 Like