S
S
svilkov872018-02-06 14:11:55
JavaScript
svilkov87, 2018-02-06 14:11:55

How to output in a loop from 1 to 10 without using variables?

Good afternoon!
In a familiar situation, we would resort to something like this construction:

for(var i = 0; i < 10; i++ ){
  console.log(i);
}

Is it possible to somehow get the numbers from 1 to 10 without resorting to the variables i ?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alex, 2018-02-06
@streetflush

console.log(1);
console.log(2);
...

S
Sergey Sokolov, 2018-02-06
@sergiks

You can loop through the array:

Array.apply(null, {length: 10}).map(function(){console.log(1+arguments[1])})
This creates an empty array of 10 elements, which are set to undefined, and for each, a function is called that displays the index of the element, increased by 1, so that not 0..9, but 1..10.
Or call the function:
(function f(){
  console.log(arguments[0]);
  if( arguments[0]<10) f(arguments[0]+1);
})(1)
But here the function itself is sort of like a variable.

A
Anatoly Zharov, 2018-02-06
@SeaBreeze876

You can use variables j , at worst k

A
Ainur Valiev, 2018-02-06
@vaajnur

Create an array with numbers from 0 - 10 by hand and output as a join

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question