1

In this example I try to print one asterisk on the first line, two on the second and so on:

var n = 5;
for (i = 0; i < n; i++) {
  for (j = 0; j < i; j++) {
    console.log('*');
  }
  console.log('\r\n');
}

The output should look like this:

*
**
***
****
*****

But in the Chrome console I see this:

*
2 *
3 *
4 *
5 *

Why is this?

2
  • 1
    If you print the same string multiple times, instead of printing it on a new line the developer console will put a number in front of the string, indicating how many times that string has been printed the same. Commented Oct 21, 2018 at 11:48
  • Because Chrome console is interactive, it's not a plain stdout character stream to which you can print strings. Commented Oct 21, 2018 at 11:56

3 Answers 3

3

console.log is not designed to precisely control the rendering of the output. It is a log writer, not a terminal layout API.

It puts each log message on a line of its own, and collapses duplicate, sequential log messages into a single message with a count of the number of times it occurred next to it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, there is one more problem, is there any Edit text in this forum for changing some text in my example?
@ASiDesigner — If you have a new question then you should ask a new question and not edit an old one.
2

It's doing that because it's how dev tools handles multiple occurrences of the same value for the purposes of clarity.

You can use an array to achieve the effect you're after. Also note that we change j<i to j<= so that it outputs on the 5th.

Then, we use the .join('') method to flatten the array into a string.

Also, since we are console.logging on every iteration loop, a new line will be added automatically as it's one log per loop.

var n = 5;
for (i = 0; i < n; i++) {
  var arr = [];
  for (j = 0; j <= i; j++) {

    arr.push('*');

  }
  console.log(arr.join(''));
}

Please note that you wouldn't need an array if you were printing this into the DOM - it would work as you originally intended it to do.

Update: Ivar made a great solution as well and that's to use the String.repeat() function which only requires you to use one loop.

var n = 5;
for (i = 1; i <= n; i++) {
  var str = '*';
  str = str.repeat(i);
  console.log(str);
}

2 Comments

In this case you could also use the String.repeat(). In that case you only need one loop.
@Ivar Great addition - I've added it to my answer.
2

Because that's the way it works, basically. Each call to console.log outputs a new log entry. Each log entry is in its own line.

If you log the same string multiple times, they will get summarized into one entry with the count in front.

Don't mistake console.log with Console.Write in C# or similar. It's more like Console.WriteLine.

2 Comments

So how to overcome this? should I do it in a string object and dump the string to the console?
@ASiDesigner Exactly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.