Skip to content

Instantly share code, notes, and snippets.

@thenasim
Created July 1, 2019 08:41
Show Gist options
  • Save thenasim/4888f9749b0475884a1716441bae001c to your computer and use it in GitHub Desktop.
Save thenasim/4888f9749b0475884a1716441bae001c to your computer and use it in GitHub Desktop.
Diamond shape with Javascript
let limit = 15; // Must be odd number
let output = '';
let middle = Math.floor(limit/2); // middle index of the limit
let indexes = []; // Indexes of where to start outputing X
let show; // how many X will be shown
let __i = 0; // to access the indexes
function log(o) {
console.log(o);
}
for (let x = middle; x >= 0; x--) {
indexes.push(x);
}
for (let z = indexes.length - 2; z >= 0; z--) { // Pushed reversed indexes to the indexes excluding 0
indexes.push(indexes[z]);
}
for (let i = 0; i < limit; i++) {
for (let j = 0; j < limit; j++) {
let index = indexes[__i];
show = limit - (index * 2);
if (j == index) {
for (let k = 0;k < show; k++) {
output += 'X';
}
j = j + show - 1;
} else {
output += '-';
}
}
log(output);
output = '';
__i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment