This commit is contained in:
Philippe Torrel
2026-09-21 10:31:46 +02:00
parent 12fdba3712
commit 6650b361c3
5 changed files with 102 additions and 32 deletions
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
@@ -26,54 +26,109 @@
let names = ['Heribert', 'Friedlinde', 'Tusnelda', 'Oswine', 'Ladislaus'];
//odd numbers
//results = inputs
console.log(results); // => [ 1, 3, 5, 7, 9 ]
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
//result = inputs
console.log(result); // => 55
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
//result = inputs
console.log(result); // => 3628800
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
result = text.split(' '); //=> ['Hi', 'this', 'is', 'a', 'short', 'text'];
//
// ['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(' '); //=> ['Hi', 'this', 'is', 'a', 'short', 'text'];
//
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 =========================
// result =
// [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 =
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
result = names.some((name) => {
return name.length > 8;
});
console.log(result); // => true
// Has every name at least 8 letters?
// result = names
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
console.log(result);
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>