feat: added 03_dom
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Übung 5: Summe einer Zahlenreihe rekursiv berechnen</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
@@ -28,10 +29,29 @@
|
||||
<script>
|
||||
'use strict';
|
||||
|
||||
const sumRecursive = (n) => {};
|
||||
const sumRecursive = (n) => {
|
||||
if (n === 0) {
|
||||
return 0;
|
||||
}
|
||||
return n + sumRecursive(n - 1); // 5 + 4 + 3 + 2 + 1 + 0
|
||||
};
|
||||
|
||||
const sumReduce = (n) => {
|
||||
return _.range(0, n + 1).reduce((a, b) => a + b, 0);
|
||||
};
|
||||
// Test Cases
|
||||
|
||||
console.log(_.range(5)); // => [0,1,2,3,4]
|
||||
console.log(_.range(2, 5)); // => [2,3,4]
|
||||
console.log(_.range(2, 10, 2)); // => [2,4,6,8]
|
||||
|
||||
console.time('recursive');
|
||||
console.log(sumRecursive(5)); // => 15 (5 + 4 + 3 + 2 + 1)
|
||||
console.timeEnd('recursive');
|
||||
|
||||
console.time('reduce');
|
||||
console.log(sumReduce(5)); // => 15 (5 + 4 + 3 + 2 + 1)
|
||||
console.timeEnd('reduce');
|
||||
console.log(sumRecursive(10)); // => 55 (10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1)
|
||||
console.log(sumRecursive(0)); // => 0
|
||||
</script>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -36,7 +36,17 @@
|
||||
<script>
|
||||
'use strict';
|
||||
|
||||
const combinations = (n, k) => {};
|
||||
const combinations = (n, k) => {
|
||||
if (k === 0 || k === n) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (k > n) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return combinations(n - 1, k - 1) + combinations(n - 1, k);
|
||||
};
|
||||
|
||||
// Test Cases
|
||||
console.log(combinations(5, 2)); // => 10
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -21,7 +21,26 @@
|
||||
<script>
|
||||
'use strict';
|
||||
|
||||
const sumNestedArray = (arr) => {};
|
||||
const sumNestedArray = (arr) => {
|
||||
// Basisfall: Wenn das Array leer ist, ist die Summe 0
|
||||
if (arr.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
// Nimm das erste Element des Arrays
|
||||
const first = arr[0];
|
||||
const rest = arr.slice(1);
|
||||
let sumFirst = 0;
|
||||
|
||||
// Wenn das erste Element ein Array ist, rufe die Funktion rekursiv auf
|
||||
if (Array.isArray(first)) {
|
||||
sumFirst = sumNestedArray(first);
|
||||
} else if (!isNaN(first)) {
|
||||
// Wenn es eine Zahl ist, füge sie zur Summe hinzu
|
||||
sumFirst = Number(first);
|
||||
}
|
||||
// Rekursiver Fall: Summe des ersten Elements + Summe des restlichen Arrays
|
||||
return sumFirst + sumNestedArray(rest);
|
||||
};
|
||||
|
||||
// Test cases
|
||||
console.log(sumNestedArray([1, [2, [3, 4], 5], 6])); // => 21
|
||||
1
02_advanced/uebungen/u26_csv-quotes2/data/csvDatei.csv
Normal file
1
02_advanced/uebungen/u26_csv-quotes2/data/csvDatei.csv
Normal file
@@ -0,0 +1 @@
|
||||
"very big, soft computer mouse","the cutest peripheral ever",10,39.90
|
||||
|
11
02_advanced/uebungen/u26_csv-quotes2/data/products.csv
Normal file
11
02_advanced/uebungen/u26_csv-quotes2/data/products.csv
Normal file
@@ -0,0 +1,11 @@
|
||||
Code,Short Description,Tagline,Quantity,Price
|
||||
MUG0007,coffee mug with LCD level indicator,never again reach for the empty mug,20,49.90
|
||||
MUG0013,coffee mug with bluetooth-connected coffee grounds scanner for automatized fortune telling,put the smart into coffee reading,20,99.90
|
||||
OFF3145,pen with pre-warmed ink (tested on the South Pole),the pen is mightier than the cold,50,29.90
|
||||
COM1001,ambidextrous computer mouse,end the dictate of left and right,10,19.90
|
||||
COM0404,Easter egg themed webcam for your monitor,put an Easter egg on hardware too,20,149.90
|
||||
COM0001,Vulcan language vi cheatsheet,learn vi and Vulcan at the same time,3,9.90
|
||||
COM1536,Klingon language emacs cheatsheet,learn emacs and Klingon at the same time,50,9.90
|
||||
MOB0555,smartphone case with built-in screen,never miss a message,20,39.90
|
||||
|
||||
|
||||
|
58
02_advanced/uebungen/u26_csv-quotes2/index-andreas.js
Normal file
58
02_advanced/uebungen/u26_csv-quotes2/index-andreas.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import fs from 'node:fs';
|
||||
import color from 'chalk';
|
||||
|
||||
const data = fs.readFileSync('data/products.csv', 'utf-8');
|
||||
const productAr = data.split('\n').map((row) => row.replace('\n', ''));
|
||||
const headerAr = productAr.shift().split(/\s*,\s*/);
|
||||
|
||||
console.log(productAr);
|
||||
|
||||
// console.log(headerAr);
|
||||
// console.log('=======');
|
||||
// console.log(productAr);
|
||||
|
||||
const convertCsvToJson = (products, headerAr) => {
|
||||
let jsonStr = '[\n';
|
||||
|
||||
products.forEach((row, idxRow) => {
|
||||
jsonStr += ' {\n';
|
||||
row
|
||||
.trim()
|
||||
.split(',')
|
||||
.forEach((elem, idxElem, ar) => {
|
||||
if (ar.length === idxElem + 1) {
|
||||
if (isNaN(elem)) {
|
||||
jsonStr += ` "${headerAr[idxElem]}": "${elem}"\n`;
|
||||
} else {
|
||||
jsonStr += ` "${headerAr[idxElem]}": ${Number(elem)}\n`;
|
||||
}
|
||||
} else {
|
||||
if (isNaN(elem)) {
|
||||
jsonStr += ` "${headerAr[idxElem]}": "${elem}",\n`;
|
||||
} else {
|
||||
jsonStr += ` "${headerAr[idxElem]}": ${Number(elem)},\n`;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (products.length === idxRow + 1) {
|
||||
jsonStr += ` }\n`;
|
||||
} else {
|
||||
jsonStr += ` },\n`;
|
||||
}
|
||||
});
|
||||
|
||||
jsonStr += ']';
|
||||
|
||||
console.log(color.yellow(`${jsonStr}`));
|
||||
|
||||
fs.writeFile('products.json', jsonStr, 'utf-8', (err) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log(color.green('file created!'));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
convertCsvToJson(productAr, headerAr);
|
||||
21
02_advanced/uebungen/u26_csv-quotes2/index-ersin.js
Normal file
21
02_advanced/uebungen/u26_csv-quotes2/index-ersin.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import fs from 'node:fs';
|
||||
import papa from 'papaparse';
|
||||
|
||||
//https://www.npmjs.com/package/papaparse
|
||||
|
||||
const data = fs.readFileSync('data/csvDatei.csv', 'utf-8').trim();
|
||||
|
||||
const parsed = papa.parse(data, {
|
||||
dynamicTyping: true,
|
||||
});
|
||||
|
||||
const dataRow = parsed.data[0];
|
||||
|
||||
const result = {
|
||||
name: dataRow[0],
|
||||
description: dataRow[1],
|
||||
quantity: dataRow[2],
|
||||
price: dataRow[3],
|
||||
};
|
||||
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
62
02_advanced/uebungen/u26_csv-quotes2/index-phil.js
Normal file
62
02_advanced/uebungen/u26_csv-quotes2/index-phil.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import fs from 'node:fs';
|
||||
import color from 'chalk';
|
||||
|
||||
const data = fs.readFileSync('data/products.csv', 'utf-8');
|
||||
|
||||
const toCamelCase = (str) => {
|
||||
return str
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[^a-zA-Z0-9]+(.)/g, (match, chr) => chr.toUpperCase());
|
||||
};
|
||||
|
||||
const convertCsvToJson = (products, ar) => {
|
||||
const resultAr = products.map((str) => {
|
||||
const fields = str.split(/\s*,\s*/);
|
||||
|
||||
const labels = ar.map((label, idx) => {
|
||||
return [label, isNaN(fields[idx]) ? fields[idx] : Number(fields[idx])];
|
||||
});
|
||||
|
||||
const obj = Object.fromEntries(labels);
|
||||
|
||||
// console.log(labels); // Entries Array
|
||||
//console.log(obj);
|
||||
|
||||
return obj;
|
||||
|
||||
// return {
|
||||
// [ar[0]]: fields[0],
|
||||
// [ar[1]]: fields[1],
|
||||
// [ar[2]]: fields[2],
|
||||
// [ar[3]]: Number(fields[3]),
|
||||
// [ar[4]]: Number(fields[4]),
|
||||
// };
|
||||
});
|
||||
// console.log(resultAr);
|
||||
|
||||
const jsonStr = JSON.stringify(resultAr, '', 2);
|
||||
|
||||
fs.writeFile('products.json', jsonStr, 'utf-8', (err) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log(color.green('file created!'));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const productAr = data
|
||||
.split('\n')
|
||||
.map((row) => row.replace('\n', ''))
|
||||
.filter((row) => row !== '');
|
||||
|
||||
const headerAr = productAr
|
||||
.shift()
|
||||
.split(/\s*,\s*/)
|
||||
.map(toCamelCase);
|
||||
|
||||
// console.log('=======');
|
||||
// console.log(productAr);
|
||||
|
||||
convertCsvToJson(productAr, headerAr);
|
||||
35
02_advanced/uebungen/u26_csv-quotes2/package-lock.json
generated
Normal file
35
02_advanced/uebungen/u26_csv-quotes2/package-lock.json
generated
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "u26_csv-quotes2",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "u26_csv-quotes2",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"chalk": "^5.6.2",
|
||||
"papaparse": "^5.5.4"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk": {
|
||||
"version": "5.6.2",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
|
||||
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": "^12.17.0 || ^14.13 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/papaparse": {
|
||||
"version": "5.5.4",
|
||||
"resolved": "https://registry.npmjs.org/papaparse/-/papaparse-5.5.4.tgz",
|
||||
"integrity": "sha512-SwzWD9gl/ElwYLCI0nUja1mFJzjq2D8ziShfNBa7zCHzkOozeOGDwHWQ+tvCzEZcewecWZ5U7kUopDnG+DFYEQ==",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
||||
17
02_advanced/uebungen/u26_csv-quotes2/package.json
Normal file
17
02_advanced/uebungen/u26_csv-quotes2/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "u26_csv-quotes2",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"chalk": "^5.6.2",
|
||||
"papaparse": "^5.5.4"
|
||||
}
|
||||
}
|
||||
58
02_advanced/uebungen/u26_csv-quotes2/products.json
Normal file
58
02_advanced/uebungen/u26_csv-quotes2/products.json
Normal file
@@ -0,0 +1,58 @@
|
||||
[
|
||||
{
|
||||
"code": "MUG0007",
|
||||
"shortDescription": "coffee mug with LCD level indicator",
|
||||
"tagline": "never again reach for the empty mug",
|
||||
"quantity": 20,
|
||||
"price": 49.9
|
||||
},
|
||||
{
|
||||
"code": "MUG0013",
|
||||
"shortDescription": "coffee mug with bluetooth-connected coffee grounds scanner for automatized fortune telling",
|
||||
"tagline": "put the smart into coffee reading",
|
||||
"quantity": 20,
|
||||
"price": 99.9
|
||||
},
|
||||
{
|
||||
"code": "OFF3145",
|
||||
"shortDescription": "pen with pre-warmed ink (tested on the South Pole)",
|
||||
"tagline": "the pen is mightier than the cold",
|
||||
"quantity": 50,
|
||||
"price": 29.9
|
||||
},
|
||||
{
|
||||
"code": "COM1001",
|
||||
"shortDescription": "ambidextrous computer mouse",
|
||||
"tagline": "end the dictate of left and right",
|
||||
"quantity": 10,
|
||||
"price": 19.9
|
||||
},
|
||||
{
|
||||
"code": "COM0404",
|
||||
"shortDescription": "Easter egg themed webcam for your monitor",
|
||||
"tagline": "put an Easter egg on hardware too",
|
||||
"quantity": 20,
|
||||
"price": 149.9
|
||||
},
|
||||
{
|
||||
"code": "COM0001",
|
||||
"shortDescription": "Vulcan language vi cheatsheet",
|
||||
"tagline": "learn vi and Vulcan at the same time",
|
||||
"quantity": 3,
|
||||
"price": 9.9
|
||||
},
|
||||
{
|
||||
"code": "COM1536",
|
||||
"shortDescription": "Klingon language emacs cheatsheet",
|
||||
"tagline": "learn emacs and Klingon at the same time",
|
||||
"quantity": 50,
|
||||
"price": 9.9
|
||||
},
|
||||
{
|
||||
"code": "MOB0555",
|
||||
"shortDescription": "smartphone case with built-in screen",
|
||||
"tagline": "never miss a message",
|
||||
"quantity": 20,
|
||||
"price": 39.9
|
||||
}
|
||||
]
|
||||
BIN
02_advanced/uebungen/u27_zlip.zip
Normal file
BIN
02_advanced/uebungen/u27_zlip.zip
Normal file
Binary file not shown.
51
02_advanced/uebungen/u27_zlip/Regex-erklaerung.md
Normal file
51
02_advanced/uebungen/u27_zlip/Regex-erklaerung.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Regex Beispiel
|
||||
|
||||
Am besten löst man das mit einem regulären Ausdruck, der entweder nach **Inhalten in Anführungszeichen** sucht _oder_ nach **Inhalten ohne Kommas**.
|
||||
|
||||
Hier ist der passende Reguläre Ausdruck:
|
||||
|
||||
`/"([^"\\]*(?:\\.[^"\\]*)*)"|([^,]+)/g`
|
||||
|
||||
---
|
||||
|
||||
### Wie der Regex funktioniert
|
||||
|
||||
Der Ausdruck nutzt das **ODER-Prinzip (`|`)**, um zwei Szenarien zu matchen:
|
||||
|
||||
1. **`"([^"\\]*(?:\\.[^"\\]*)*)"` (Gruppe 1):** Matcht alles, was in doppelten Anführungszeichen steht. Er erlaubt sogar escaped Anführungszeichen (`\"`) im Text.
|
||||
2. **`([^,]+)` (Gruppe 2):** Wenn keine Anführungszeichen da sind, matcht er alles bis zum nächsten Komma.
|
||||
|
||||
---
|
||||
|
||||
### Anwendung in JavaScript (Beispiel)
|
||||
|
||||
Da du das Ergebnis sicherlich in einem Array von einzelnen Werten brauchst, kannst du den Regex wie folgt anwenden. Hierbei nutzen wir `matchAll`, um die Treffer sauber zu trennen und die Anführungszeichen direkt zu entfernen:
|
||||
|
||||
```javascript
|
||||
const input = '"very big, soft computer mouse","the cutest peripheral ever",10,39.90';
|
||||
const regex = /"([^"\\]*(?:\\.[^"\\]*)*)"|([^,]+)/g;
|
||||
|
||||
const matches = [...input.matchAll(regex)];
|
||||
|
||||
const result = matches.map((match) => {
|
||||
// Wenn Gruppe 1 existiert, war es ein String in Anführungszeichen
|
||||
// Wenn Gruppe 2 existiert, war es ein unberührter Wert (Zahl)
|
||||
const value = match[1] !== undefined ? match[1] : match[2].trim();
|
||||
|
||||
// Optional: Datentypen konvertieren
|
||||
if (!isNaN(value) && value !== '') {
|
||||
return Number(value);
|
||||
}
|
||||
return value;
|
||||
});
|
||||
|
||||
console.log(result);
|
||||
```
|
||||
|
||||
### Das Ergebnis (Output)
|
||||
|
||||
Nach dem Parsen erhältst du ein sauberes Array, bei dem die Kommas innerhalb der Strings ignoriert wurden und die Zahlen einsatzbereit sind:
|
||||
|
||||
```json
|
||||
["very big, soft computer mouse", "the cutest peripheral ever", 10, 39.9]
|
||||
```
|
||||
1
02_advanced/uebungen/u27_zlip/data/csvDatei.csv
Normal file
1
02_advanced/uebungen/u27_zlip/data/csvDatei.csv
Normal file
@@ -0,0 +1 @@
|
||||
"very big, soft computer mouse","the cutest peripheral ever",10,39.90
|
||||
|
11
02_advanced/uebungen/u27_zlip/data/products.csv
Normal file
11
02_advanced/uebungen/u27_zlip/data/products.csv
Normal file
@@ -0,0 +1,11 @@
|
||||
Code,Short Description,Tagline,Quantity,Price
|
||||
MUG0007,coffee mug with LCD level indicator,never again reach for the empty mug,20,49.90
|
||||
MUG0013,coffee mug with bluetooth-connected coffee grounds scanner for automatized fortune telling,put the smart into coffee reading,20,99.90
|
||||
OFF3145,pen with pre-warmed ink (tested on the South Pole),the pen is mightier than the cold,50,29.90
|
||||
COM1001,ambidextrous computer mouse,end the dictate of left and right,10,19.90
|
||||
COM0404,Easter egg themed webcam for your monitor,put an Easter egg on hardware too,20,149.90
|
||||
COM0001,Vulcan language vi cheatsheet,learn vi and Vulcan at the same time,3,9.90
|
||||
COM1536,Klingon language emacs cheatsheet,learn emacs and Klingon at the same time,50,9.90
|
||||
MOB0555,smartphone case with built-in screen,never miss a message,20,39.90
|
||||
|
||||
|
||||
|
63
02_advanced/uebungen/u27_zlip/index.js
Normal file
63
02_advanced/uebungen/u27_zlip/index.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import fs from 'node:fs';
|
||||
import zlib from 'node:zlib';
|
||||
import color from 'chalk';
|
||||
|
||||
const data = fs.readFileSync('data/products.csv', 'utf-8');
|
||||
|
||||
const toCamelCase = (str) => {
|
||||
return str
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[^a-zA-Z0-9]+(.)/g, (match, chr) => chr.toUpperCase());
|
||||
};
|
||||
|
||||
const convertCsvToJson = (products, ar) => {
|
||||
const resultAr = products.map((str) => {
|
||||
const fields = str.split(/\s*,\s*/);
|
||||
|
||||
const labels = ar.map((label, idx) => {
|
||||
return [label, isNaN(fields[idx]) ? fields[idx] : Number(fields[idx])];
|
||||
});
|
||||
|
||||
const obj = Object.fromEntries(labels);
|
||||
|
||||
// console.log(labels); // Entries Array
|
||||
//console.log(obj);
|
||||
|
||||
return obj;
|
||||
|
||||
// return {
|
||||
// [ar[0]]: fields[0],
|
||||
// [ar[1]]: fields[1],
|
||||
// [ar[2]]: fields[2],
|
||||
// [ar[3]]: Number(fields[3]),
|
||||
// [ar[4]]: Number(fields[4]),
|
||||
// };
|
||||
});
|
||||
// console.log(resultAr);
|
||||
|
||||
const jsonStr = JSON.stringify(resultAr, '', 2);
|
||||
|
||||
fs.writeFile('products.json.gz', zlib.gzipSync(jsonStr), 'utf-8', (err) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log(color.green('file created!'));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const productAr = data
|
||||
.split('\n')
|
||||
.map((row) => row.replace('\n', ''))
|
||||
.filter((row) => row !== '');
|
||||
|
||||
const headerAr = productAr
|
||||
.shift()
|
||||
.split(/\s*,\s*/)
|
||||
.map(toCamelCase);
|
||||
|
||||
// console.log('=======');
|
||||
// console.log(productAr);
|
||||
|
||||
convertCsvToJson(productAr, headerAr);
|
||||
17
02_advanced/uebungen/u27_zlip/package.json
Normal file
17
02_advanced/uebungen/u27_zlip/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "u27_zlib",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"chalk": "^5.6.2",
|
||||
"papaparse": "^5.5.4"
|
||||
}
|
||||
}
|
||||
BIN
02_advanced/uebungen/u27_zlip/products.json.gz
Normal file
BIN
02_advanced/uebungen/u27_zlip/products.json.gz
Normal file
Binary file not shown.
54
02_advanced/uebungen/u28_password-generator/index.js
Normal file
54
02_advanced/uebungen/u28_password-generator/index.js
Normal file
@@ -0,0 +1,54 @@
|
||||
// const crypto = require('node:crypto');
|
||||
import crypto from 'node:crypto';
|
||||
import _ from 'lodash';
|
||||
|
||||
const PASSWORD_LENGTH = 10;
|
||||
const s = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ!.,;#$%/+*';
|
||||
|
||||
const buf = crypto.randomBytes(PASSWORD_LENGTH);
|
||||
|
||||
console.log(buf, Array.from(buf));
|
||||
|
||||
const password = Array.from(buf)
|
||||
.map((byte) => s.charAt(byte % s.length))
|
||||
.join('');
|
||||
|
||||
const getPassword = (amount = 10) => {
|
||||
const chars = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ!.,;#$%/+*'.split('');
|
||||
return _.shuffle(chars).slice(0, amount).join('');
|
||||
};
|
||||
|
||||
console.log(password);
|
||||
console.log(getPassword());
|
||||
|
||||
// Source - https://stackoverflow.com/a/2450976
|
||||
// Posted by ChristopheD, modified by community. See post 'Timeline' for change history
|
||||
// Retrieved 2026-07-10, License - CC BY-SA 4.0
|
||||
|
||||
function shuffle(array) {
|
||||
let currentIndex = array.length;
|
||||
|
||||
// While there remain elements to shuffle...
|
||||
while (currentIndex != 0) {
|
||||
// Pick a remaining element...
|
||||
let randomIndex = Math.floor(Math.random() * currentIndex);
|
||||
currentIndex--;
|
||||
|
||||
// And swap it with the current element.
|
||||
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// Used like so
|
||||
let arr = [2, 11, 37, 42];
|
||||
shuffle(arr);
|
||||
console.log(arr);
|
||||
|
||||
// Übung 28: Passwortgenerator
|
||||
|
||||
// Was macht der folgende Code?
|
||||
|
||||
// OK: Der Titel verrät es schon. Versuche dennoch, das Programm zu verstehen! Benutze die Dokumentation der Standardbibliothek, um nachzuschlagen, was crypto.randomBytes(…) genau macht!
|
||||
|
||||
// Bonusfrage: Warum sind in den Zeichen «1», «i» und «l» nicht enthalten, genau so wenig wie «0» und «o»?
|
||||
21
02_advanced/uebungen/u28_password-generator/package-lock.json
generated
Normal file
21
02_advanced/uebungen/u28_password-generator/package-lock.json
generated
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "u28_password-generator",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "u28_password-generator",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"lodash": "^4.18.1"
|
||||
}
|
||||
},
|
||||
"node_modules/lodash": {
|
||||
"version": "4.18.1",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.18.1.tgz",
|
||||
"integrity": "sha512-dMInicTPVE8d1e5otfwmmjlxkZoUpiVLwyeTdUsi/Caj/gfzzblBcCE5sRHV/AsjuCmxWrte2TNGSYuCeCq+0Q==",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
||||
14
02_advanced/uebungen/u28_password-generator/package.json
Normal file
14
02_advanced/uebungen/u28_password-generator/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "u28_password-generator",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"lodash": "^4.18.1"
|
||||
}
|
||||
}
|
||||
BIN
02_advanced/uebungen/u29_stream.zip
Normal file
BIN
02_advanced/uebungen/u29_stream.zip
Normal file
Binary file not shown.
8769
02_advanced/uebungen/u29_stream/data/products.html
Normal file
8769
02_advanced/uebungen/u29_stream/data/products.html
Normal file
File diff suppressed because it is too large
Load Diff
BIN
02_advanced/uebungen/u29_stream/data/products.html.gz
Normal file
BIN
02_advanced/uebungen/u29_stream/data/products.html.gz
Normal file
Binary file not shown.
21
02_advanced/uebungen/u29_stream/index.js
Normal file
21
02_advanced/uebungen/u29_stream/index.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// const fs = require('node:fs');
|
||||
// const zlib = require('node:zlib');
|
||||
import fs from 'node:fs';
|
||||
import zlib from 'node:zlib';
|
||||
|
||||
const gzipCompressor = zlib.createGzip();
|
||||
|
||||
const inputStream = fs.createReadStream('data/products.html');
|
||||
const outputStream = fs.createWriteStream('data/products.html.gz');
|
||||
|
||||
inputStream.on('data', (data) => {
|
||||
console.log('Pakete (data chunks) werden geladen: ', data.length);
|
||||
//outputStream.write(zlib.gzipSync(data));
|
||||
});
|
||||
|
||||
inputStream.pipe(gzipCompressor).pipe(outputStream);
|
||||
|
||||
// Übung 29: Datenströme
|
||||
// Schreibe Codebeispiel 239 um, ohne die Funktion pipe(…) zu verwenden!
|
||||
|
||||
// TIPP: Um eine Datei päckchenweise lesen- und schreiben zu können, gibt es für das fs Modul die Methoden createReadStream und createWriteStream.
|
||||
13
02_advanced/uebungen/u29_stream/package.json
Normal file
13
02_advanced/uebungen/u29_stream/package.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "03_stream",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "module"
|
||||
}
|
||||
BIN
02_advanced/uebungen/u32_os-info.zip
Normal file
BIN
02_advanced/uebungen/u32_os-info.zip
Normal file
Binary file not shown.
35
02_advanced/uebungen/u32_os-info/index.js
Normal file
35
02_advanced/uebungen/u32_os-info/index.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import os from 'node:os';
|
||||
import color from 'chalk';
|
||||
|
||||
const platform = os.platform();
|
||||
|
||||
const cpu = os.cpus().length;
|
||||
|
||||
console.log(color.yellow(`I am running on a machine with ${platform} and ${cpu} cores.`));
|
||||
|
||||
//console.log(os.cpus()); //gibt ein array aus jeweils cpu kernen
|
||||
console.log(os.type()); // Darwin
|
||||
console.log(os.version()); // Darwin mit kernelversion
|
||||
console.log(os.totalmem()); //RAM in bytes
|
||||
console.log(os.freemem());
|
||||
console.log(os.availableParallelism()); // 8 -> ein programm sollte max 8 parallele tasks ausführen?
|
||||
console.log(os.arch()); // cpu architektur -> arm64
|
||||
console.log(os.machine()); // arm64
|
||||
//console.log(os.networkInterfaces());
|
||||
console.log(os.hostname()); //MacBookAir
|
||||
console.log(os.release()); // die releasenr und nicht datum
|
||||
console.log(os.userInfo()); // Object mit Userinfos
|
||||
console.log(os.userInfo().username);
|
||||
|
||||
// Übung 32: Information zum Betriebssystem und der Anzahl der vorhandenen CPUs
|
||||
|
||||
// Im Kontext einer Monitoring-Lösung benötigt ein Kunde eine kleine CLI (Command Line Interface)-Anwendung, die es erlaubt, das aktuelle Betriebssystem und die Anzahl der CPUs auf dem jeweiligen Rechner zu ermitteln.
|
||||
|
||||
// Vorgehensweise
|
||||
|
||||
// 1.Schau dir die Online-Dokumentation zum os-Modul der Standardbibliothek an.
|
||||
// 2.Erstelle dann ein kurzes Node.js-Programm, das ausgibt, auf welcher Plattform es gerade läuft (also etwa »linux«, »darwin« (OS X) oder »win32«) und wie viele Kerne der Rechner hat.
|
||||
|
||||
// Hier eine mögliche Beispielausgabe:
|
||||
|
||||
// I am running on a machine with linux and 2 cores.
|
||||
27
02_advanced/uebungen/u32_os-info/package-lock.json
generated
Normal file
27
02_advanced/uebungen/u32_os-info/package-lock.json
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "u32_os-info",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "u32_os-info",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"chalk": "^5.6.2"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk": {
|
||||
"version": "5.6.2",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
|
||||
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": "^12.17.0 || ^14.13 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
14
02_advanced/uebungen/u32_os-info/package.json
Normal file
14
02_advanced/uebungen/u32_os-info/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "u32_os-info",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"chalk": "^5.6.2"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user