This commit is contained in:
Philippe Torrel
2026-09-16 13:14:37 +02:00
parent c31196e216
commit 2a00da2551
2 changed files with 122 additions and 0 deletions
@@ -0,0 +1,72 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Zylindervolumen berechnen (tube) mit Guard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<main>
<div class="container py-5">
<h1>Zylindervolumen berechnen (tube) mit Guard</h1>
</div>
</main>
<script>
'use strict';
const BASE_COST_PER_UNIT = 0.7;
const COST_PER_CC = 0.001;
const height = 80;
const diameter = 10;
/**
* Funktion zum Berechnen vom Zylindervolumen
*/
const calcTubeVolume = (diameter = 10, height = 80) => {
// Guard
if (isNaN(diameter) || isNaN(height) || diameter < 0 || height < 0) return NaN;
const radius = diameter / 2;
// cylindricalVolume = π × radius² × height
const volume = Math.PI * Math.pow(radius, 2) * height;
return volume;
};
/**
* Funktion zum Berechnen vom Zylindervolumen (Kurzschreibweise)
*/
const calcTubeVolumeShort = (diameter, height) =>
isNaN(diameter) || isNaN(height) || diameter < 0 || height < 0
? NaN
: Math.PI * Math.pow(diameter / 2, 2) * height;
/**
* Funktion zum Berechnen der Versandkosten
*/
const calcShippingCost = (volume) => {
// Guard
if (isNaN(volume)) {
console.warn('wrong volume data type');
return NaN;
}
// shippingCost = volume × costPerCC + costPerUnit
const cost = volume * COST_PER_CC + BASE_COST_PER_UNIT;
return cost;
};
/**
* Funktion zum Berechnen der Versandkosten (Kurzschreibweise)
*/
const calcShippingCostShort = (volume) => (isNaN(volume) ? NaN : volume * COST_PER_CC + BASE_COST_PER_UNIT);
const tubeVolume = calcTubeVolume(); // calcTubeVolume(diameter, height);
const shippingCost = calcShippingCost(tubeVolume);
console.log(shippingCost); // => 6.983185307179587
console.log(calcShippingCostShort(calcTubeVolumeShort(10, 80))); // => 6.983185307179587
</script>
</body>
</html>
@@ -0,0 +1,50 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rest- und Defaultparameter</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<main>
<div class="container py-5">
<h1>Rest- und Defaultparameter</h1>
</div>
</main>
<script>
'use strict';
// Defaultparameter
const add = (n1 = 0, n2 = 0) => {
return Number(n1) + Number(n2);
};
console.log(add(1, 2)); // => 3
console.log(add(5, '12')); // => 17
console.log(add(1)); // => 1
console.log(add()); // => 0
// Restparameter (...)
const sum = (...numbers) => {
return numbers.reduce((a, b) => Number(a) + Number(b), 0);
};
console.log(sum(1, 2)); // => 3
console.log(sum(1, 2, 3, 4, 5)); // => 15
console.log(sum(10, 20, 30)); // => 60
// Restparameter (...names)
const greetWith = (greeting, ...names) => {
names.forEach((name) => {
console.log(`${greeting}, ${name}!`);
});
// return `${greeting}, ${names}!`;
};
greetWith('Hey', 'Goldy');
greetWith('Hi', 'Stefan', 'Florian', 'Benjamin', 'Chrsitian', 'Philippe');
</script>
</body>
</html>