Learn how to print the numbers from 0 or 1 to 100 without including any numeric value in your JavaScript code.

How to print the numbers from 1 to 100 without including numbers in your JavaScript code

asdfghjk, yep, this is the kind of questions that some developers found on coding interviews or tests that you can find on platforms like codingame, codewars and others. Of course, this isn't the kind of thing that you will need on your job daily, however it's quite fun as it forces you to use the creativity in realtime.

The point of this question, is to print the numbers from a specific range, usually from 0 to 100 without typing any numeric value in your code. Crazy right? As mentioned, usually you will have to print the numbers from 0 to 100. So, in JavaScript it would be so easy as:

for(let i = 0;i <= 100;i++){
    console.log(i);
}

However, there are 2 numbers in the implementation, 0 and 100, which isn't allowed. In this short article, I will show you a nice approach that you can use to solve the problem with this question using JavaScript (the same logic may be used in another programming language as well). 

From 0 to 100

If you want to print numbers, of course, you need to get them from somewhere. The simplest approach is to obtain a numeric value from the length of a variable, either an array or a string which are the simplest ones. So, for example:

// This would output: 1
console.log(".".length);

You can start from that, right? So the length of a string with 3 dots, would be equivalent to the number 3, number that you can use as range in some loop. In this case, to print the numbers from 0 to 100 without typing any number in JavaScript, the following code would work perfectly:

let max = "..........".length;

for(let i = [].length;i <= (max * max);i++){
    console.log(i);
}

In this approach, as start point we will have 0, defined by the length of an empty array. As limit, we define a numeric value that will be 10, stored in the max variable, and then we will simply multiply it by the same variable, so 10x10=100, and that's the limit of the loop.

From 1 to 100

If instead of 0, you need to start from 1, simply use the length of a string with a dot as the start point:

let max = "..........".length;

for(let i = ".".length;i <= (max * max);i++){
    console.log(i);
}

From X to N

If the range that you need to print is different, just play with some math and the length of diverse strings, for example, if you need to print the numbers between 27 to 105, you could end up with something like:

// start = 9 * 3 = 27
let start = ".........".length * "...".length;
// limit = 10 * 10 + 5 = 105
let limit = "..........".length * "..........".length + ".....".length;

for(let i = start;i <= limit;i++){
    console.log(i);
}

Your imagination is the limit 😂.

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors