Digits of a number when iterating through a loop in JavaScript

Suppose that when iterating over numbers, we want to get some digit from these numbers. For example, we want to make a number be displayed on the console if its first digit is 1 or 2.

I remind you that you can’t refer to the characters of a number directly, that is, it won’t work like this:

for (let i = 1; i <= 100; i++) { if (i[0] == 1 || i[0] == 2) { // that won't work console.log(i); } }

First you need to convert the number to a string and then get a certain character of the returned string, like this:

for (let i = 1; i <= 100; i++) { let str = String(i); // converting number to string if (str[0] === '1' || str[0] === '2') { console.log(i); } }

Loop through the numbers from 10 to 1000 and output the first digit of each number to the console.

Loop through the numbers from 10 to 1000 and output the sum of the first and second digits of each number to the console.

Loop through the numbers from 10 to 1000 and output those numbers whose first digit is equal to 1.

Loop through the numbers from 10 to 1000 and display those numbers whose sum of the first two digits is 5.

enru