diff --git a/01_grundlagen/uebungen/u46_richtige-gewinner.html b/01_grundlagen/uebungen/u46_richtige-gewinner.html index f8f5d4c..ad9800c 100644 --- a/01_grundlagen/uebungen/u46_richtige-gewinner.html +++ b/01_grundlagen/uebungen/u46_richtige-gewinner.html @@ -34,11 +34,15 @@ 'use strict'; const outputEl = document.querySelector('.output'); - const winners = ['Heribert', 'Friedlinde', 'Tusnelda', 'Oswine', 'Ladislaus']; + const labels = ['1st', '2nd', '3rd']; // Beispiel - const getWinners = (winners) => {}; + const getWinners = (winners) => { + return winners.map((winner, index) => { + return index < labels.length ? `${labels[index]} place: ${winner}` : winner; + }); + }; console.log(getWinners(winners)); outputEl.innerHTML = ``; diff --git a/01_grundlagen/uebungen/u47_higher-order-fn-lueckentext.html b/01_grundlagen/uebungen/u47_higher-order-fn-lueckentext.html index 8b0f6d2..42cb2e1 100644 --- a/01_grundlagen/uebungen/u47_higher-order-fn-lueckentext.html +++ b/01_grundlagen/uebungen/u47_higher-order-fn-lueckentext.html @@ -1,4 +1,4 @@ - + @@ -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; diff --git a/01_grundlagen/uebungen/u48_froehliches-mixen.html b/01_grundlagen/uebungen/u48_froehliches-mixen.html index 9508b77..93532e2 100644 --- a/01_grundlagen/uebungen/u48_froehliches-mixen.html +++ b/01_grundlagen/uebungen/u48_froehliches-mixen.html @@ -1,4 +1,4 @@ - + @@ -28,7 +28,13 @@ diff --git a/01_grundlagen/unterricht/tag10/09_local-scoping-iife.html b/01_grundlagen/unterricht/tag10/09_local-scoping-iife.html index ab964d8..071d9e9 100644 --- a/01_grundlagen/unterricht/tag10/09_local-scoping-iife.html +++ b/01_grundlagen/unterricht/tag10/09_local-scoping-iife.html @@ -67,7 +67,10 @@