Javascript for contour printing a contour in one line

I am trying to get the output from a for loop to print on a single line in the console.

for(var i = 1; i < 11; i += 1) { console.log(i); } 

Now it

 1 2 3 4 5 6 7 8 9 10 

How can I get the output on one line (e.g. 1 2 3 4 5 6 7 8 9 10 )?

+5
source share
3 answers

Create a line, then write it after the loop.

 var s = ""; for(var i = 1; i < 11; i += 1) { s += i + " "; } console.log(s); 
+15
source

No problem, just combine them in one line:

 var result = ''; for(var i = 1; i < 11; i += 1) { result = result + i; } console.log(result) 

or better

 console.log(Array.apply(null, {length: 10}).map(function(el, index){ return index; }).join(' ')); 

Go on and learn! Good luck

+2
source

There may be an alternative way to print counters on a single line, console.log () places the final new line without an indication, and we cannot skip this.

 let str = '',i=1; while(i<=10){ str += i+''; i += 1; } console.log(str); 
0
source

Source: https://habr.com/ru/post/1233554/


All Articles