This commit is contained in:
@@ -1,6 +1,34 @@
|
||||
// const getFileBy = (path, encoding = 'utf-8') => {
|
||||
import fs from 'node:fs';
|
||||
|
||||
// };
|
||||
const getFileBy = (path = '', encoding = 'utf-8') => {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(path, encoding, (error, data) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Promise.all([
|
||||
getFileBy('./data/file01.txt'),
|
||||
getFileBy('./data/file02.txt'),
|
||||
getFileBy('./data/file03.txt'),
|
||||
// getFileBy('./data/file_03.txt'),
|
||||
getFileBy('./data/file04.txt'),
|
||||
getFileBy('./data/file05.txt'),
|
||||
getFileBy('./data/file06.txt'),
|
||||
])
|
||||
.then((data) =>
|
||||
data.forEach((data) => {
|
||||
console.log(data);
|
||||
}),
|
||||
)
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
|
||||
@@ -1,24 +1,43 @@
|
||||
// TODO: imports
|
||||
import dns from 'node:dns';
|
||||
import color from 'chalk';
|
||||
import util from 'node:util';
|
||||
|
||||
const IP_V = 4; // we use IP protocol version 4
|
||||
const URL = 'google.de';
|
||||
|
||||
// TODO: your modifications
|
||||
|
||||
// 1.
|
||||
// const getIpBy = () => {};
|
||||
const getIpBy = (url, ip = 4) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
dns.lookup(url, ip, (error, data) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve({ address: data, family: ip });
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 2.
|
||||
// Promises Unterobjekt von Node
|
||||
// const getPromisesIpBy = () => {};
|
||||
const getPromisesIpBy = (url, ip = 4) => {
|
||||
return dns.promises.lookup(url, ip);
|
||||
};
|
||||
|
||||
// 3.
|
||||
// Promisify Helferfunktion
|
||||
// const getPromisifyIpBy =
|
||||
|
||||
const getPromisifyIpBy = util.promisify(dns.lookup);
|
||||
// 4.
|
||||
// Async await
|
||||
// const getAsyncIpBy =
|
||||
const getAsyncIpBy = async (url, ip) => {
|
||||
try {
|
||||
const result = await dns.promises.lookup(url, ip);
|
||||
return result;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw new Error(err);
|
||||
}
|
||||
};
|
||||
|
||||
// Test Cases:
|
||||
// =============================
|
||||
@@ -55,23 +74,20 @@ getAsyncIpBy(URL, IP_V)
|
||||
});
|
||||
|
||||
/* Übung 13: Das Telefonbuch des Internets
|
||||
|
||||
Vielleicht hast du einmal die Webseite www.nodejs.com besucht? Dann hat dein Computer die so genannte IP-Adresse des Servers www.nodejs.com herausgefunden.
|
||||
|
||||
|
||||
Vielleicht hast du einmal die Webseite www.nodejs.com besucht? Dann hat dein Computer die so genannte IP-Adresse des Servers www.nodejs.org herausgefunden.
|
||||
|
||||
Ähnlich wie ein Telefonbuch ordnet das Domain Name System (DNS) jedem Server (Host) eine eindeutige IP-Adresse zu, unter der er über das Internet erreichbar ist.
|
||||
|
||||
|
||||
Die Node.js-API bietet eine Funktion, um Namen im DNS nachzuschlagen: dns.lookup(…).
|
||||
|
||||
|
||||
Die Übung besteht nun darin, obiges Beispiel so umzuschreiben, dass der Aufruf von dns.lookup(…) in eine Wrapper-Funkion verpackt wird.
|
||||
|
||||
|
||||
Es sollen drei verschiedene Lösungen erstellt werden:
|
||||
|
||||
|
||||
1. Ausführliche Variante mit eigenem Promise-Objekt als Ummantelung
|
||||
|
||||
2. Kurze Variante mit Promises-Unterobjekt (dns.promises)
|
||||
|
||||
2. Kurze Variante mit Promises-Unterobjekt (dns.promises)
|
||||
3. Promisify-Variante mit util.promisify(…)
|
||||
|
||||
4. Async await Variante
|
||||
|
||||
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "u13_dns-lookup",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "u13_dns-lookup",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,43 @@
|
||||
['Clara', 76, 95, 89, 91],
|
||||
['David', 82, 79, 91, 85],
|
||||
];
|
||||
|
||||
const calculateAvgGrades = () => {
|
||||
const averages = [];
|
||||
|
||||
for (let row = 1; row < gradesTable.length; row++) {
|
||||
let sum = 0;
|
||||
|
||||
for (let column = 1; column < gradesTable[row].length; column++) {
|
||||
sum += gradesTable[row][column];
|
||||
}
|
||||
|
||||
const numberOfSubjects = gradesTable[row].length - 1;
|
||||
averages.push(Number((sum / numberOfSubjects).toFixed(2)));
|
||||
}
|
||||
|
||||
return averages;
|
||||
};
|
||||
|
||||
const calculateAvgSubjects = () => {
|
||||
const averages = [];
|
||||
|
||||
for (let column = 1; column < gradesTable[0].length; column++) {
|
||||
let sum = 0;
|
||||
|
||||
for (let row = 1; row < gradesTable.length; row++) {
|
||||
sum += gradesTable[row][column];
|
||||
}
|
||||
|
||||
const numberOfStudents = gradesTable.length - 1;
|
||||
averages.push(Number((sum / numberOfStudents).toFixed(2)));
|
||||
}
|
||||
|
||||
return averages;
|
||||
};
|
||||
|
||||
console.log(calculateAvgGrades()); // => [85.75, 85.25, 87.75, 84.25]
|
||||
console.log(calculateAvgSubjects()); // => [83.25, 88.5, 85.5, 85.75]
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -32,7 +32,24 @@
|
||||
['Dairy', 'Milk', 'Cheese', 'Yogurt'],
|
||||
];
|
||||
|
||||
// Your code here
|
||||
shoppingList[0][2] = 'Grapes'; // Replace 'Bananas' with 'Grapes'
|
||||
shoppingList[1][2] = 'Tomatoes'; // Replace 'Broccoli' with 'Tomatoes'
|
||||
shoppingList[2][2] = 'Butter'; // Replace 'Cheese' with 'Butter'
|
||||
|
||||
// let replaceAr = ['Grapes', 'Tomatoes', 'Butter'];
|
||||
|
||||
// const results = shoppingList.map((list, idx) => {
|
||||
// return list.toSpliced(2, 1, replaceAr[idx]);
|
||||
// });
|
||||
|
||||
// console.log(results);
|
||||
|
||||
// Expected outcome:
|
||||
// [
|
||||
// ['Fruits', 'Apples', 'Grapes', 'Oranges'],
|
||||
// ['Vegetables', 'Carrots', 'Tomatoes', 'Spinach'],
|
||||
// ['Dairy', 'Milk', 'Butter', 'Yogurt'],
|
||||
// ]
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -36,7 +36,21 @@
|
||||
['Russia', 'Moscow'],
|
||||
];
|
||||
|
||||
const getCapitalOf = () => {};
|
||||
const getCapitalOf = (country) => {
|
||||
return countriesWithCapital.find((countryWithCapital) => countryWithCapital[0] === country)[1];
|
||||
};
|
||||
|
||||
// const capitalOf = (country) => {
|
||||
// const capitalIndex = 1;
|
||||
// const countryIndex = 0;
|
||||
// return countriesWithCapital.find((countryWithCapital) => countryWithCapital[countryIndex] === country)[
|
||||
// capitalIndex
|
||||
// ];
|
||||
// };
|
||||
|
||||
console.log('Switzerland:', getCapitalOf('Switzerland'));
|
||||
console.log('Germany:', getCapitalOf('Germany'));
|
||||
console.log('Russia:', getCapitalOf('Russia'));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -39,7 +39,11 @@
|
||||
],
|
||||
];
|
||||
|
||||
// Your code here
|
||||
// Add the Books category with its products.
|
||||
inventory.push([
|
||||
['Shelf 1', 'Novels', 40],
|
||||
['Shelf 2', 'Non-fiction', 35],
|
||||
]);
|
||||
|
||||
console.log(inventory);
|
||||
/* Expected outcome:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -44,7 +44,7 @@
|
||||
],
|
||||
];
|
||||
|
||||
// Your code here
|
||||
inventory[1][1][2] = 25;
|
||||
|
||||
console.log(inventory);
|
||||
/*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -41,7 +41,13 @@
|
||||
];
|
||||
|
||||
const increaseBrightness = (image, value) => {
|
||||
// Your code here
|
||||
return image.map((row) =>
|
||||
//
|
||||
row.map((pixel) =>
|
||||
//
|
||||
pixel.map((idx) => Math.min(idx + value, 255)),
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
const newImage = increaseBrightness(image, 30);
|
||||
|
||||
Reference in New Issue
Block a user