Programming best practice with loops related to numeral converter project

To make this message easier for me to write and make sure I am not missing anything I am not going to avoid stating the likely obvious.

My current code for the Build a Roman Numeral Converter certification project contain something that I want to check whether is programming best practice. For clarity I have passed the project.
The programs contains a while loop wit in a for loop . This while loop, I know for certain iterations* of the for loop, regardless of the inputted number is effectively an if loop. For other iterations* depending on my inputted value, a while loop will not even be entered. The code I have written is shorter and from at least once perspective easier to understand however for reasons such as processor time, being more specific to the problem is it better to write a longer code.

Alternatively if for some other reason my approach is not the best feel free to say. It is notable to me how little of what I have learned since the previous project I am using. The inbuilt function to convert a integer to a string is all. Excluding insight that draws on what was cover in the recent programs but you can know without them. I have thought of if I can use recursive functions but do not think that is the best way.

In case my terminology is wrong. I am considering a new iteration to start when for loop code is repeated, with the value the iterable (in my case of 'i ') changing.

hello, you can always share snippets (not entire solution unless you keep it in spoiler mode, if you find thats relevant to show your entire process) from your codebase as well, with more comments to describe your issue for clarity…

happy coding :slight_smile:

1 Like

There is often a tradeoff between shorter code and easily readable code. In general at this stage don’t worry about it. Write code that works and passes the tests.

“the best” way is something you will learn with practice and is honestly generally hotly debated in any case.

A loop is a loop. Sometimes with a different structure you might save 1 cycle by having a condition check at the top or bottom. Usually saving one cycle is totally irrelevant, but it could be a factor one day in something computationally heavy.

There could be a situation where you always want the loop to execute at least once, so you want a check at the bottom (do/while) but you can often re-write the condition to achieve this.

You might find this video interesting:

1 Like

pkdvalis has sort already answered the question but since you sort of requested it, here is a code segments. As this is the sort of question that can be answered in different ways.

const romanNumerals=["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"];
  const romanNumeralsEquiv=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
  output.innerText="";
for(let i=0; i<13; i++){
    console.log("i is",i);
    while (arabicNumeral>=romanNumeralsEquiv[i]){
      output.innerText+=romanNumerals[i];
      arabicNumeral=arabicNumeral-romanNumeralsEquiv[i];
    }
  }

I have not seen the video, as yet but what you say is helpful. I may say more another day when I have had time to think over my words.

Generally I would say not to get bogged down in these details, even if they are interesting. Just keep moving forward because it’s a long road.

When you are far down the road you can look back with more clarity on these little details.

Yes I was getting carried, try to explain how what you said was helpful and explaining more where my thought process was when I sent the message and in the past. This not important to you. I will say what you said is broadly in line with what I thought and have been taught in the past outside of freeCodeCamp but I was starting to question.
On my response to bappyasif if you are refering to this, I decided as he/she/they seemed to be up for providing more specific help I decided to not turn him down. However while my word choice did really reflect this help was not necessary but not without benefits.