135 lines
4.3 KiB
HTML
135 lines
4.3 KiB
HTML
<!doctype html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Übung 47: Lückentext zu Higher-Order-Funktionen</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>Übung 47: Lückentext zu Higher-Order-Funktionen</h1>
|
|
<p>
|
|
Das ist garantiert der letzte Lückentext für diese Lektion. Ersetzen Sie wieder die Kommentare
|
|
<strong>/* ??? */</strong> durch den richtigen Code, um das angegebene Ergebnis zu erzielen.
|
|
</p>
|
|
</div>
|
|
</main>
|
|
<script>
|
|
'use strict';
|
|
|
|
let result;
|
|
let results;
|
|
let inputs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
let text = 'Hi this is a short text';
|
|
let names = ['Heribert', 'Friedlinde', 'Tusnelda', 'Oswine', 'Ladislaus'];
|
|
|
|
//odd numbers
|
|
let resultsOddNumber = inputs.filter((n) => {
|
|
if (n % 2 !== 0) {
|
|
return true;
|
|
}
|
|
});
|
|
|
|
// resultsOddNumber = inputs.filter((n) => n % 2 !== 0);
|
|
|
|
console.log(resultsOddNumber); // => [ 1, 3, 5, 7, 9 ]
|
|
|
|
//sum
|
|
let resultSum = inputs.reduce((sum, n) => {
|
|
return sum + Number(n);
|
|
}, 0);
|
|
|
|
// resultSum = inputs.reduce((sum, n) => sum + Number(n), 0);
|
|
console.log(resultSum); // => 55
|
|
|
|
//product
|
|
let resultProduct = inputs.reduce((sum, n) => {
|
|
return sum * Number(n);
|
|
}, 1);
|
|
|
|
// resultProduct = inputs.reduce((sum, n) => sum * Number(n), 1);
|
|
console.log(resultProduct); // => 3628800
|
|
|
|
//longest word length
|
|
|
|
// ['Hi', 'this', 'is', 'a', 'short', 'text'];
|
|
|
|
result = text.split(' ').reduce((length, str) => {
|
|
return str.length > length ? str.length : length;
|
|
}, 0);
|
|
|
|
result = text
|
|
.split(' ') // ['Hi', 'this', 'is', 'a', 'short', 'text'];
|
|
.map((word) => word.length) // [2, 4, 2, 1, 5, 4];
|
|
.reduce((longest, word) => Math.max(longest, word), 0);
|
|
// => ['Hi', 'this', 'is', 'a', 'short', 'text'];
|
|
|
|
console.log(result); // => 5
|
|
|
|
//longest word
|
|
result = text.split(' ').filter((str) => str.length === result)[0];
|
|
|
|
result = text.split(' ').reduce((res, word) => {
|
|
return res.length > word.length ? res : word;
|
|
}, '');
|
|
//=> ['Hi', 'this', 'is', 'a', 'short', 'text'];
|
|
console.log(result); // =>'short'
|
|
|
|
//avg word length =========================
|
|
|
|
// [0 , 'Hi', 'this', 'is', 'a', 'short', 'text'];
|
|
result = text.split(' ').reduce((sum, str, idx, arr) => {
|
|
const sumStrLength = sum + str.length;
|
|
let returnValue = 0;
|
|
|
|
return idx === arr.length - 1 ? sumStrLength / arr.length : sumStrLength;
|
|
}, 0);
|
|
|
|
result =
|
|
text
|
|
.split(' ') // => ['Hi', 'this', 'is', 'a', 'short', 'text'];
|
|
.map((x) => x.length) // => [2, 4, 2, 1, 5, 4];
|
|
.reduce((sum, x) => sum + x, 0) / text.split(' ').length;
|
|
|
|
result = text.replaceAll(' ', '').length / text.split(' ').length;
|
|
|
|
console.log(result); // => 3
|
|
|
|
//sort by 3rd letter
|
|
// ['Heribert', 'Friedlinde', 'Tusnelda', 'Oswine', 'Ladislaus'];
|
|
|
|
const names2 = [...names];
|
|
results = names2.sort((a, b) => (a.charAt(2) > b.charAt(2) ? 1 : -1));
|
|
|
|
console.log(results); // => [ 'Ladislaus', 'Friedlinde', 'Heribert', 'Tusnelda', 'Oswine' ]
|
|
|
|
// Are there names with more than 8 letters?
|
|
// ['Heribert', 'Friedlinde', 'Tusnelda', 'Oswine', 'Ladislaus'];
|
|
|
|
result = names.some((name) => {
|
|
return name.length > 8;
|
|
});
|
|
console.log(result); // => true
|
|
|
|
// Has every name at least 8 letters?
|
|
result = names.every((name) => {
|
|
return name.length >= 8;
|
|
});
|
|
console.log(result); // false
|
|
|
|
// What is the lowest value from the inputs?
|
|
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
result = inputs.sort((a, b) => a - b).at(0);
|
|
// result = inputs.sort((a, b) => a - b)[0];
|
|
|
|
result = inputs.reduce((n1, n2) => (n1 < n2 ? n1 : n2));
|
|
|
|
result = Math.min(...inputs);
|
|
|
|
console.log(result); // => 1;
|
|
</script>
|
|
</body>
|
|
</html>
|