QPLOT

Welcome to the first blog. Enjoy :)

Programming

How to display a square shape?

How to display a square shape?

How to display a square shape?

Problem

Problem

Problem

Print a square shape of size n using symbol *.

Examples

n = 32
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
********************************
n = 8
********
********
********
********
********
********
********
********
n = 3
***
***
***
n = 1
*

Solution:

Step 1

Start with a function named square that takes an input parameter n:

function square(n) {

}

Step 2

Create a line composed of n characters:

function square(n) {

const line = new Array(n).fill('*').join('')

}

Step 3

Repeat the same line n times:

function square(n) {

const line = new Array(n).fill('*').join('')

const lines = new Array(n).fill(line)

Step 4

In the end, join all lines and return as a string.

function square(n) {

const line = new Array(n).fill('*').join('')

const lines = new Array(n).fill(line)

return lines.join('\n')

}

Try it!

Try it!

Try it!

Play with the interactive version below. (Under Development)

n = 64
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************

QPLOT 2023

END