Question: I just wrote a code that draws a bordered square. 

Given the square size of 5 for example, the code should print the following multi-line string:

```
#####
#   #
#   #
#   #
#####

```

and I was wondering if there's anyway of simplifying this more and reduce time complexity. 

```js
var BoxFiller = "#";
var BoxSize = 8;
var Row = "";

function DrawSquare() {

  for (i = 0; i < BoxSize; i++) {
    var r = BoxSize - 1;

    if (i == 0 || i == r) {
      Row = BoxFiller.repeat(BoxSize);
    } else {
      Row = BoxFiller + " ".repeat(BoxSize - 2) + BoxFiller;
    }

    console.log(Row);

  }
}

DrawSquare();
```

Answer: 
This is an alternative which reduces Space and Time complexity.

*Time complexity: `O(n)`*

```js
var BoxFiller = "#";
var BoxSize = 8;
var Row = "";
var spaces = " ".repeat(BoxSize - 2);
var hashtags = BoxFiller.repeat(BoxSize);

function DrawSquare() {  
  console.log(hashtags);

  for (var i = 1; i < BoxSize - 1; i++) {
    console.log(BoxFiller + spaces + BoxFiller);
  }

  console.log(hashtags);
}

DrawSquare();
```

```css
.as-console-wrapper { max-height: 100% !important; top: 0; }
```
Slight alteration to @Ele's version that allows for both a size and a filler to be passed in.

```js
function DrawSquare(size, filler) { 
  const line = filler.repeat(size);
  const mid = filler+' '.repeat(size-2)+filler;
  let lines = [line];

  for (var i = 1; i < size - 1; i++) {
    lines.push(mid);
  }

  lines.push(line);

  console.log(lines.join('\n'));
}

DrawSquare(8, '*');
```

If I was going to redraw the same thing several times I would change it to look like this:

```js
const cache = {};

function DrawSquare(size, filler) {
  if (!cache[size]) {
    const line = filler.repeat(size);
    const mid = filler+' '.repeat(size-2)+filler;
    let lines = [line];

    for (var i = 1; i < size - 1; i++) {
      lines.push(mid);
    }

    lines.push(line);

    cache[size] = lines.join('\n');
  }

  console.log(cache[size]);
}

console.time('first');
DrawSquare(12, '*');
console.timeEnd('first');

console.time('second');
DrawSquare(12, '*');
console.timeEnd('second');
```