62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
// Variante 1
|
|
|
|
const sentence = 'Dies ist eine Übung um der callback Hölle zu entkommen.';
|
|
|
|
const printWords = (sentence) => {
|
|
const wordArr = sentence.split(' ');
|
|
|
|
for (let i = 0; i < wordArr.length; i++) {
|
|
setTimeout(() => {
|
|
console.log(wordArr[i]);
|
|
}, 1000 * i);
|
|
}
|
|
};
|
|
|
|
const printWordsRek = (sentence, idxWort = 0) => {
|
|
const wordArr = sentence.split(' ');
|
|
|
|
// Basis-Fall
|
|
if (idxWort >= wordArr.length) {
|
|
return '';
|
|
}
|
|
|
|
// rekursiver mist :)
|
|
setTimeout(() => {
|
|
printWordsRek(sentence, idxWort + 1);
|
|
}, 1000);
|
|
|
|
// Ausgabe des Wortes
|
|
console.log(wordArr[idxWort]);
|
|
};
|
|
|
|
printWords(sentence);
|
|
printWordsRek(sentence);
|
|
|
|
// setTimeout(() => {
|
|
// console.log('The');
|
|
|
|
// setTimeout(() => {
|
|
// console.log('pyramid');
|
|
|
|
// setTimeout(() => {
|
|
// console.log('of');
|
|
|
|
// setTimeout(() => {
|
|
// console.log('doom');
|
|
|
|
// setTimeout(() => {
|
|
// console.log('keeps');
|
|
|
|
// setTimeout(() => {
|
|
// console.log('growing.');
|
|
// }, 1000);
|
|
// }, 1000);
|
|
// }, 1000);
|
|
// }, 1000);
|
|
// }, 1000);
|
|
// }, 1000);
|
|
|
|
// Übung 11: Die Pyramide abbrechen
|
|
|
|
// Schreibe das Codebeispiel 96 so um, dass keine Pyramide entsteht. Benutze dazu, wie im Text angedeutet, eine Funktion, die das n-te Wort ausgibt und sich per setTimeout(…) mit n + 1 als Parameter aufruft, solange noch Wörter im Satz vorhanden sind.
|