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

Tell us what’s happening:

I’ve started the roman numeral converter project and think I’m actually understanding how to do it, however I’m wondering should I continue using an “else if” statement, as I think technically it’ll work, but unsure if it’s the cleanest way to write it?

Not expecting an answer, just wanting to make sure I’m going down the right path! As my understanding is, although I can get it to return the correct value for each input from the user story, it won’t give infinite answers to any number using an “else if” statement. (Hope this makes sense!)

Your code so far

/* file: script.js */
const convertBtn = document.getElementById('convert-btn');
const input = document.getElementById('number');
const output = document.getElementById('output');

convertBtn.addEventListener("click", function() {
  if (input.value === "") {
    output.textContent = "Please enter a valid number";
  } else if (input.value <= -1) {
    output.textContent = "Please enter a number greater than or equal to 1";
  } else if (input.value >= 4000) {
    output.textContent = "Please enter a number less than or equal to 3999";
  }
})

Your browser information:

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

Challenge Information:

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

Personally, i practice clean code way like this :laughing:
→ finish project/ work → review and make it cleaner while still passing the test → try read other work/solution

So the experience gained, will slowly, make it an instinct.

There is many styles to code thanks to javascript beauty, for example , your code can be rewrite into this:

output.textContent = !input.value ?  "Please enter a valid number" 
    : Number(input.value) <= -1 ? "Please enter a number greater than or equal to 1"
    : Number(input.value) >= 4000 ?  "Please enter a number less than or equal to 3999"
    : output.textContent   // else,  no change

But may sacrifice some readability/maintainability.

I recommend scan thru this Javascript style when you are feel more confident with javascript. → GitHub - airbnb/javascript: JavaScript Style Guide

2 Likes

I don’t think a chained ternary like that is really what I’d call ‘cleaner’. That’s more personal preference, but usually it’s not recommended.

I get what you saying but every single number between 1 to 3999 should work for you to ACTUALLY complete the project, i also did it the way you did but i’m going back to try correct it

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

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