Drawing a bordered square with least time complexity in JS
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.
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();
javascript time-complexity
add a comment |
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.
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();
javascript time-complexity
2
I think this question would be more suitable posted on Code Review Stack Exchange
– NewToJS
Nov 23 '18 at 1:55
1
There is, and this is not the place to ask. Follow @NewToJS's advice. This is a request for help improving already working code.
– Mike 'Pomax' Kamermans
Nov 23 '18 at 1:57
add a comment |
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.
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();
javascript time-complexity
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.
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();
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();
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();
javascript time-complexity
javascript time-complexity
edited Nov 23 '18 at 1:52
Ele
22.8k42045
22.8k42045
asked Nov 23 '18 at 1:49
CoreDoCoreDo
98831018
98831018
2
I think this question would be more suitable posted on Code Review Stack Exchange
– NewToJS
Nov 23 '18 at 1:55
1
There is, and this is not the place to ask. Follow @NewToJS's advice. This is a request for help improving already working code.
– Mike 'Pomax' Kamermans
Nov 23 '18 at 1:57
add a comment |
2
I think this question would be more suitable posted on Code Review Stack Exchange
– NewToJS
Nov 23 '18 at 1:55
1
There is, and this is not the place to ask. Follow @NewToJS's advice. This is a request for help improving already working code.
– Mike 'Pomax' Kamermans
Nov 23 '18 at 1:57
2
2
I think this question would be more suitable posted on Code Review Stack Exchange
– NewToJS
Nov 23 '18 at 1:55
I think this question would be more suitable posted on Code Review Stack Exchange
– NewToJS
Nov 23 '18 at 1:55
1
1
There is, and this is not the place to ask. Follow @NewToJS's advice. This is a request for help improving already working code.
– Mike 'Pomax' Kamermans
Nov 23 '18 at 1:57
There is, and this is not the place to ask. Follow @NewToJS's advice. This is a request for help improving already working code.
– Mike 'Pomax' Kamermans
Nov 23 '18 at 1:57
add a comment |
3 Answers
3
active
oldest
votes
This is an alternative which reduces Space and Time complexity.
Time complexity: O(n)
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();
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
Can use recursion
let BoxFiller = "#";
let Row = "";
var spaces, hashtags;
function DrawRow(BoxSize, CurrPos) {
if(CurrPos >= BoxSize - 2)
return;
console.log(BoxFiller + spaces + BoxFiller);
DrawRow(BoxSize, CurrPos + 1);
}
function DrawSquare(BoxSize) {
spaces = " ".repeat(BoxSize - 2);
hashtags = BoxFiller.repeat(BoxSize);
console.log(hashtags);
DrawRow(BoxSize, 0);
console.log(hashtags);
}
DrawSquare(8);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
Slight alteration to @Ele's version that allows for both a size and a filler to be passed in.
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:
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');
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439780%2fdrawing-a-bordered-square-with-least-time-complexity-in-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is an alternative which reduces Space and Time complexity.
Time complexity: O(n)
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();
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
This is an alternative which reduces Space and Time complexity.
Time complexity: O(n)
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();
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
This is an alternative which reduces Space and Time complexity.
Time complexity: O(n)
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();
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is an alternative which reduces Space and Time complexity.
Time complexity: O(n)
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();
.as-console-wrapper { max-height: 100% !important; top: 0; }
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();
.as-console-wrapper { max-height: 100% !important; top: 0; }
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();
.as-console-wrapper { max-height: 100% !important; top: 0; }
edited Nov 23 '18 at 15:59
answered Nov 23 '18 at 2:04
EleEle
22.8k42045
22.8k42045
add a comment |
add a comment |
Can use recursion
let BoxFiller = "#";
let Row = "";
var spaces, hashtags;
function DrawRow(BoxSize, CurrPos) {
if(CurrPos >= BoxSize - 2)
return;
console.log(BoxFiller + spaces + BoxFiller);
DrawRow(BoxSize, CurrPos + 1);
}
function DrawSquare(BoxSize) {
spaces = " ".repeat(BoxSize - 2);
hashtags = BoxFiller.repeat(BoxSize);
console.log(hashtags);
DrawRow(BoxSize, 0);
console.log(hashtags);
}
DrawSquare(8);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
Can use recursion
let BoxFiller = "#";
let Row = "";
var spaces, hashtags;
function DrawRow(BoxSize, CurrPos) {
if(CurrPos >= BoxSize - 2)
return;
console.log(BoxFiller + spaces + BoxFiller);
DrawRow(BoxSize, CurrPos + 1);
}
function DrawSquare(BoxSize) {
spaces = " ".repeat(BoxSize - 2);
hashtags = BoxFiller.repeat(BoxSize);
console.log(hashtags);
DrawRow(BoxSize, 0);
console.log(hashtags);
}
DrawSquare(8);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
Can use recursion
let BoxFiller = "#";
let Row = "";
var spaces, hashtags;
function DrawRow(BoxSize, CurrPos) {
if(CurrPos >= BoxSize - 2)
return;
console.log(BoxFiller + spaces + BoxFiller);
DrawRow(BoxSize, CurrPos + 1);
}
function DrawSquare(BoxSize) {
spaces = " ".repeat(BoxSize - 2);
hashtags = BoxFiller.repeat(BoxSize);
console.log(hashtags);
DrawRow(BoxSize, 0);
console.log(hashtags);
}
DrawSquare(8);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Can use recursion
let BoxFiller = "#";
let Row = "";
var spaces, hashtags;
function DrawRow(BoxSize, CurrPos) {
if(CurrPos >= BoxSize - 2)
return;
console.log(BoxFiller + spaces + BoxFiller);
DrawRow(BoxSize, CurrPos + 1);
}
function DrawSquare(BoxSize) {
spaces = " ".repeat(BoxSize - 2);
hashtags = BoxFiller.repeat(BoxSize);
console.log(hashtags);
DrawRow(BoxSize, 0);
console.log(hashtags);
}
DrawSquare(8);
.as-console-wrapper { max-height: 100% !important; top: 0; }
let BoxFiller = "#";
let Row = "";
var spaces, hashtags;
function DrawRow(BoxSize, CurrPos) {
if(CurrPos >= BoxSize - 2)
return;
console.log(BoxFiller + spaces + BoxFiller);
DrawRow(BoxSize, CurrPos + 1);
}
function DrawSquare(BoxSize) {
spaces = " ".repeat(BoxSize - 2);
hashtags = BoxFiller.repeat(BoxSize);
console.log(hashtags);
DrawRow(BoxSize, 0);
console.log(hashtags);
}
DrawSquare(8);
.as-console-wrapper { max-height: 100% !important; top: 0; }
let BoxFiller = "#";
let Row = "";
var spaces, hashtags;
function DrawRow(BoxSize, CurrPos) {
if(CurrPos >= BoxSize - 2)
return;
console.log(BoxFiller + spaces + BoxFiller);
DrawRow(BoxSize, CurrPos + 1);
}
function DrawSquare(BoxSize) {
spaces = " ".repeat(BoxSize - 2);
hashtags = BoxFiller.repeat(BoxSize);
console.log(hashtags);
DrawRow(BoxSize, 0);
console.log(hashtags);
}
DrawSquare(8);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Nov 23 '18 at 2:58
Miller Cy ChanMiller Cy Chan
22929
22929
add a comment |
add a comment |
Slight alteration to @Ele's version that allows for both a size and a filler to be passed in.
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:
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');
add a comment |
Slight alteration to @Ele's version that allows for both a size and a filler to be passed in.
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:
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');
add a comment |
Slight alteration to @Ele's version that allows for both a size and a filler to be passed in.
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:
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');
Slight alteration to @Ele's version that allows for both a size and a filler to be passed in.
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:
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');
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, '*');
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, '*');
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');
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');
answered Nov 23 '18 at 3:17
IntervaliaIntervalia
4,25611033
4,25611033
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439780%2fdrawing-a-bordered-square-with-least-time-complexity-in-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I think this question would be more suitable posted on Code Review Stack Exchange
– NewToJS
Nov 23 '18 at 1:55
1
There is, and this is not the place to ask. Follow @NewToJS's advice. This is a request for help improving already working code.
– Mike 'Pomax' Kamermans
Nov 23 '18 at 1:57