Iterating Through an Array with a "Value-Based" For Loop

Iterating Through an Array with a "Value-Based" For Loop
0

#1

I am trying the following in the Roman Numeral Converter Project:

var num = 36;

var arabic = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var roman = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];


for (var i = 0; num > arabic[i]; i++) {
    console.log(i);
}

I am just logging i in an attempt to see whether this works or not. It is not. Why would something like this not work?


#2

First it sets i to 0.
Then it checks whether num is greater than arabic[i]. 36 is not greater than 1000, so the loop exits.


#3

Wow, I got so absorbed in the minutia of the code, that I literally couldn’t see that! Thank you for your help. :slight_smile: