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.
/**
* 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
}