JavaScript Algorithms and Data Structures Projects: Roman Numeral Converter - help

when we use for loop in an array example for(let i = 0; i < arr.length; i++) and we want to loop from the beginning again we put i = 0 so we can loop from the beginning of the array again like so

for(let i = 0; i < arr.length; i++){
  i = 0;
}

my question is very simple : how can i do the same thing with the

for(let key in obj)

with an object instead of an array ??

**See my code is suppose to go back to the beginning of the object objj in order to compare num with objj[key] again but i don’t know how to do it. **


function convertToRoman(num) {
 let objj ={
   M: 1000 ,
   D: 500 ,
   C: 100 ,
   L: 50 ,
   X: 10 ,
   V: 5 ,
   I: 1  
 }
 let arr = [];
 for(let key in objj){
   if(num >= objj[key]){
     arr.push(key);
     num = num - objj[key];
   }
   
 }
return arr.join("");
}

console.log(convertToRoman(36));

Your browser information:

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

Challenge: Roman Numeral Converter

Link to the challenge:

So you want to keep this for-in loop running while this condition: (num >= objj[key]) is true, correct? :slight_smile:

1 Like

yes ! thats exactly what i want to do . but i dont know how to do it with the for in loop

I think I’ve answered your question already :wink:

i see what you did there lol. the thing is i’m still new to javascript so i’m not quite sure how to put that together

/**
 * For any given key in the object
 * condition (num >= objj[key]) must be true
 * which means num must be greater or equal to 1
 * In other words, you need your loop running
 * while num is positive integer greater than 0
 */
while (num >= 1) {
  // loop
}
2 Likes

*I’m afraid this will only answer your question but not really solve the challenge problem though :slight_smile:

1 Like
while( num >= 1){
 for(let key in objj){

That way ?

it’s okay . as long as i’m learning something new solving the challenge doesn’t matter to me at least for now . thanks man i appreciate the help !!