Skip to content
Advertisement

Display squares of asterisks, filled and hollow, side by side

I’m currently trying to display a filled asterisk square and a hollow asterisk :

JavaScript

Instead, I’m getting this output:

JavaScript

I’m not so sure how to make it side by side instead of up and down. Do I have to put the hollow nested loop inside the filled loop or do I have to do something else? Here is my code:

JavaScript

Advertisement

Answer

John Martinez’ answer works fine, but it’s a little inefficient. I’ll put a step by step below your code, which I revamped:

JavaScript

Step 1: Convert your two loops to one loop. This is because you cannot print on the same line once you’ve made a new line.

Step2: Because we’ll be using Strings in a loop, it’s more efficient to use a StringBuffer, so we don’t constantly make new strings

Step 3: Write all of the output from your logic to the buffers instead of the console.

Step 4: Print the buffers one line at a time, as we fill them.

Step 5: Profit!

Advertisement