Skip to content

Instantly share code, notes, and snippets.

@puttputt
Last active April 24, 2019 23:32
Show Gist options
  • Save puttputt/cd7279337d77fc11be629946cb6829c6 to your computer and use it in GitHub Desktop.
Save puttputt/cd7279337d77fc11be629946cb6829c6 to your computer and use it in GitHub Desktop.
tedx presentation: diamond shape problem
// usage:
// node diamond.js 8
//
const length = parseInt(process.argv.slice(2)[0]);
const char = '*';
const space = ' ';
const newLine = '\n';
const TWO = 2;
var charCount = 1;
var spaceCount = length - 1;
var adding = true;
process.stdout.write(newLine);
for (var i = 0; i < length; i++) {
drawLine(spaceCount, charCount);
charCount = charCount + TWO;
spaceCount--;
}
charCount = charCount - TWO;
spaceCount++;
for (var i = 0; i < length - 1; i++) {
charCount = charCount - TWO;
spaceCount++;
drawLine(spaceCount, charCount);
}
process.stdout.write(newLine);
function drawLine(spaceCount, charCount) {
for (var i = 0; i < spaceCount; i++) {
process.stdout.write(space);
}
for (var i = 0; i < charCount; i++) {
process.stdout.write(char);
}
process.stdout.write(newLine);
}
@puttputt
Copy link
Author

As featured in my TEDx talk:
https://www.youtube.com/watch?v=x77-gT8bWLo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment