Build a Roman Numeral Converter Project - Build a Roman Numeral Converter

This is what I could come up with…

solution removed by moderator

@Zneider

please do not provide answers.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

We’ve got a lot of good stuff going on there, but it will need a little bit of tweaking.

function convertOnesToRoman(num) {
  const romanNum = [
    {symbol: I, value: 1},
    {symbol: IV, value: 4},
    {symbol: V, value: 5},
    {symbol: IX, value: 9},
  ]

  let result = "";
  for (let i = 0; i > 0; i++) {
    if (i < 0) {
      break;
    }
    romanNum + 1 === result;
  }
}

We’ve got some things to change though

  1. Nothing is being returned. We need to use the return keyword to get an answer out of this function

  2. Your loop never runs.

  3. You never update the result variable.

  4. You don’t use your romanNum variable.

I think point 1 and 4 are good to focus on. Can you modify your romanNum array to hold all Roman numberals from 1 to 9? If so, you could look up the result in that array an return it.

function convertOnesToRoman(num) {
  const romanNum = [
    {symbol: I, value: 1},
    {symbol: II,  value: 2},
    {symbol: III, value: 3},
    {symbol: IV, value: 4},
    {symbol: V, value: 5},
   {symbol: VI, value: 6},
  {symbol: VII, value: 7},
 {symbol: VIII, value: 8},
 {symbol: IX, value: 9},
  ]

  let result = "";
  for (let i = 0; i < 9; i++) {
    if (i > 9) {
      break;
    }
    return result = romanNum + i;
  }
}

I don’t understand how exactly you intend for this to work? I think you might not fully understand how return statements or for loops work.

I’d review these Steps

I completed the two lessons you sent. Thank so much for these!
I think I get it a bit better now.

But I still don’t know what I want the loop to do to convert the numbers into roman numerals.

I don’t think you need a loop.

function convertOnesToRoman(num) {

}

When num is 1, convertOnesToRoman should return I. Can you make that happen? (The simplest thing to make that happen is totally fine here!)

by doing this

function convertOnesToRoman(num) {
if (num === 1) {
return "I"; 
}

Hmm, close, but console.log doesn’t return anything.

Changed it. Can you take a look?

Minor tweak, you forgot a }

function convertOnesToRoman(num) {
  if (num === 1) {
    return "I"; 
  }
}

This will work. What about if num === 2 (or 3, or 4)?

function convertOnesToRoman(num) {
if (num === 1||num === 2 || num === 3 ) {
return “I”;
return “II”;
return “III”;
}
}

Hmm, that won’t work. The first valid return statement triggers and the rest won’t. I think you need separate if statements here.

function convertOnesToRoman(num) {
  if (num === 1) {
    return "I"; 
  } 
if (num === 2) {
return "II";
} 
if (num === 3) {
return "III";
} 
}
function convertOnesToRoman(num) {
  if (num === 1) {
    return "I";
  }
  if (num === 2) {
    return "II";
  }
  if (num === 3) {
    return "III";
  }
}

Don’t forget to use indentation!

This will work - can you handle the rest of the numbers 4-9 here?

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

function convertOnesToRoman(num) {
  if (num === 1) {
    return "I";
  }
  if (num === 2) {
    return "II";
  }
  if (num === 3) {
    return "III";
  } 
if (num === 4) {
     return "IV";
    }
if (num === 5) {
      return "V";
       }
if (num === 6) {
      return "VI";
       }
if (num === 7) {
      return "VII";
       }
if (num === 8) {
      return "VIII";
       }
if (num === 5) {
      return "IX";
       }
}

Cool, small typo there but this is a very helpful function.

function convertOnesToRoman(num) {
  if (num === 1) {
    return "I";
  }
  if (num === 2) {
    return "II";
  }
  if (num === 3) {
    return "III";
  } 
  if (num === 4) {
    return "IV";
  }
  if (num === 5) {
    return "V";
  }
  if (num === 6) {
    return "VI";
  }
  if (num === 7) {
    return "VII";
  }
  if (num === 8) {
    return "VIII";
  }
  if (num === 9) {
    return "IX";
  }
}

One more small issue here and we’ll be done with this function - what should we do for 0?

I have it in my code above. Output displays a warning if the user submits things like -1, 0 or 4000