Skip to content

Instantly share code, notes, and snippets.

@ripsware
Created October 12, 2018 07:16
Show Gist options
  • Save ripsware/a9a7db149479b098e528a5e5a37a72b6 to your computer and use it in GitHub Desktop.
Save ripsware/a9a7db149479b098e528a5e5a37a72b6 to your computer and use it in GitHub Desktop.
(() => {
const lineChars = ["x", " ", "o", " "];
function createLine(length, isReverse){
let result = "";
for(let counter = 0; counter < length; counter++){
if(isReverse){
result = result + lineChars[counter % lineChars.length];
}else{
result = lineChars[counter % lineChars.length] + result;
}
}
return result;
}
function buildShape(n){
const max = n * 2 - 1;
let result = "";
for(let i = 1; i <= max; i++){
if(i < n){
result += "".padStart(n - 1) + createLine(i);
}else if(i == n){
result += createLine(n, true) + createLine(n - 1);
}else{
result += "".padStart(i - n) + createLine(n - (i - n) , true);
}
result += "\n";
}
return result;
}
(() => {
console.log(buildShape(20));
})();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment