Last active
April 24, 2019 23:32
-
-
Save puttputt/cd7279337d77fc11be629946cb6829c6 to your computer and use it in GitHub Desktop.
tedx presentation: diamond shape problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As featured in my TEDx talk:
https://www.youtube.com/watch?v=x77-gT8bWLo