This commit is contained in:
@@ -201,7 +201,6 @@ Abschlussquiz - JS Grundlagen
|
|||||||
|
|
||||||
- Promises
|
- Promises
|
||||||
- Callback-Hell
|
- Callback-Hell
|
||||||
-
|
|
||||||
- Promise Aufbau
|
- Promise Aufbau
|
||||||
- Promise in Funktionen
|
- Promise in Funktionen
|
||||||
- File System von Node
|
- File System von Node
|
||||||
|
|||||||
@@ -16,7 +16,8 @@
|
|||||||
</h2>
|
</h2>
|
||||||
<p>
|
<p>
|
||||||
Schreibe eine rekursive Funktion in JavaScript, die die Summe aller Zahlen in einem verschachtelten Array
|
Schreibe eine rekursive Funktion in JavaScript, die die Summe aller Zahlen in einem verschachtelten Array
|
||||||
berechnet. Das Array kann beliebig tief verschachtelt sein und enthält nur Zahlen oder weitere Arrays.
|
berechnet. Das Array kann beliebig tief verschachtelt sein und sollte nur Zahlen oder weitere Arrays
|
||||||
|
enthalten.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -24,7 +25,19 @@
|
|||||||
<script>
|
<script>
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const sumNestedArray = (arr) => {};
|
const sumNestedArray = (arr) => {
|
||||||
|
let sum = 0;
|
||||||
|
|
||||||
|
arr.forEach((value) => {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
sum += sumNestedArray(value); // 1 + 2 + 3 +
|
||||||
|
} else if (typeof value === 'number') {
|
||||||
|
sum += value; // 4 + 5 + 6
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
};
|
||||||
|
|
||||||
// Test cases
|
// Test cases
|
||||||
console.log(sumNestedArray([1, [2, [3, 4], 5], 6])); // => 21
|
console.log(sumNestedArray([1, [2, [3, 4], 5], 6])); // => 21
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
// Variante 1
|
||||||
|
|
||||||
|
const printWords = (text = '', idx = 0) => {
|
||||||
|
const words = text.split(' '); //=> ['KI-Entwickler', 'warnen', 'UN-Gremium']
|
||||||
|
|
||||||
|
// Basisfall
|
||||||
|
if (idx >= words.length) return; // Abbruch
|
||||||
|
|
||||||
|
console.log(words[idx]); // KI-Entwickler
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
printWords(text, ++idx);
|
||||||
|
}, 1000);
|
||||||
|
};
|
||||||
|
|
||||||
|
// printWords('KI-Entwickler warnen UN-Gremium');
|
||||||
|
|
||||||
|
const printWordsFor = (sentence) => {
|
||||||
|
const wordArr = sentence.split(' ');
|
||||||
|
|
||||||
|
for (let i = 0; i < wordArr.length; i++) {
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log(wordArr[i]);
|
||||||
|
}, 1000 * i);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
printWordsFor('Bundestag berät über Organspende');
|
||||||
|
// 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.
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
name, category, price
|
||||||
|
Klingon Letter Opener, Office Warfare, 19.99
|
||||||
|
Backpack of Holding, Travel, 29.99
|
||||||
|
Tardis Alarmclock, Merchandise, 15.99
|
||||||
|
@@ -0,0 +1,59 @@
|
|||||||
|
// fs = FileSystem Modul aus der Node Standardbib (core)
|
||||||
|
import fs from 'node:fs'; // module - ESM Schreibweise
|
||||||
|
import util from 'node:util';
|
||||||
|
import color from 'chalk';
|
||||||
|
|
||||||
|
// Funktion mit einem asynchronen Prozess
|
||||||
|
const getFileBy = (path = '', encoding = 'utf-8') => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// async process
|
||||||
|
fs.readFile(path, encoding, (error, data) => {
|
||||||
|
if (error) {
|
||||||
|
// console.error(error);
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// console.log(data);
|
||||||
|
resolve(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const getPromisesFileBy = (path = '', encoding = 'utf-8') => {
|
||||||
|
// Unterobjekt "promises" von fs besitzt Ummantelung von Promise-Objekt und die Verarbeitung der Callback-Funktion
|
||||||
|
|
||||||
|
// async process
|
||||||
|
return fs.promises.readFile(path, encoding);
|
||||||
|
};
|
||||||
|
|
||||||
|
// util.promisify fängt das letzte Argument als Callback-Funktion ab und ummantel die Funktion mit einem Promise-Objekt.
|
||||||
|
// Das ummantelte Promise-Objekt wird zusätzlich von einer weiteren Funktion mit den Argumenten ummantelt.
|
||||||
|
|
||||||
|
// https://javascript.info/promisify
|
||||||
|
// https://medium.com/trabe/understanding-nodes-promisify-and-callbackify-d2b04efde0e0
|
||||||
|
const getPromisifyFileBy = util.promisify(fs.readFile);
|
||||||
|
|
||||||
|
// Funktionsaufruf einer Funktion mit asynchronem Prozess
|
||||||
|
getFileBy('./data/products.csv')
|
||||||
|
.then((data) => {
|
||||||
|
console.log(color.yellow(data));
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(color.red(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
getPromisesFileBy('./data/products.csv')
|
||||||
|
.then((data) => {
|
||||||
|
console.log(color.yellow(data));
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(color.red(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
getPromisifyFileBy('./data/products.csv', 'utf-8')
|
||||||
|
.then((data) => {
|
||||||
|
console.log(color.yellow(data));
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(color.red(err));
|
||||||
|
});
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"name": "05_promise-readfile",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"name": "05_promise-readfile",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"dependencies": {
|
||||||
|
"chalk": "^6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/chalk": {
|
||||||
|
"version": "6.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.0.tgz",
|
||||||
|
"integrity": "sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=22"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "07_fs-promises",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"type": "module",
|
||||||
|
"dependencies": {
|
||||||
|
"chalk": "^6.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user