Skip to content

Instantly share code, notes, and snippets.

@behrends
Last active October 21, 2020 12:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behrends/9524fa7dbc683a2d003c2c06004d61e0 to your computer and use it in GitHub Desktop.
Save behrends/9524fa7dbc683a2d003c2c06004d61e0 to your computer and use it in GitHub Desktop.
const lines = parseInt(prompt("Anzahl der Zeilen: "));
if(isNaN(lines)) {
console.log('Bitte Zahl eingeben!');
} else {
// Weihnachtsbaum
for(let i = 1; i <= lines; i++) {
// Anzahl der Sterne berechnen (STARS)
// Zeile 1 = i : * 1 = i + 0 = 1 + 0 = i + (i - 1)
// Zeile 2 = i : *** 3 = i + 1 = 2 + 1 = i + (i - 1)
// Zeile 3 = i : ***** 5 = i + 2 = 3 + 2 = i + (i - 1)
// Einrückung mit Leerzeichen berechnen (SPACES)
// Zeile 1 = i : * Einrückung: 2 = lines-1 = lines-i
// Zeile 2 = i : *** Einrückung: 1 = lines-2 = lines-i
// Zeile 3 = i : ***** Einrückung: 0 = lines-3 = lines-i
let stars = '';
// Sterne einrücken bzw. Leerzeichen ausgeben (siehe SPACES oben)
for(let k = 0; k < lines - i; k++) { // ' ' * (lines - i)
stars = stars + ' ';
}
// erweitere stars mit i + i-1 Sternen (siehe STARS oben)
for(let j = 0; j < (i + i -1); j++) { // '*' * (i + i -1)
stars = stars + '*';
}
console.log(stars);
}
}
// Einrücken auf die Mitte der letzten Zeile
// for-Schleife von 1 bis lines-1 für Leerzeichen
let trunk = '';
for(let l = 1; l < lines; l++) {
trunk = trunk + ' ';
}
console.log(trunk + 'I');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment