Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hauptsächlich obj vs json bis u mit aufgabe 4 #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions meineLoesungen/closures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 1
// a. Was gibt folgender Code aus?

for (var i = 0; i < 4; i++) {
setTimeout(() => {
console.log(i)
}, 2000)
}

// b. Wie kann ich das Programm ändern, damit es tut was es tun sollte.
34 changes: 34 additions & 0 deletions meineLoesungen/flow-control-funktional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Soll die Quersumme 13 ergeben
const a = [1,34,5, 0, -4]
// let b = a.reduce((a,sum) => sum + a)
// console.log(b) // 40 => Falsch


// https://bobbyhadz.com/blog/javascript-split-number-into-array
let b = a.reduce((sum,a) => {
if(a > 0 && a < 10){
return sum + a
}
if(a >= 10){
let digits = Array.from(String(a), Number)
return sum + digits.reduce((d,s) => d+s)
}
return sum
})
console.log(b)

// Funktional:
function quersumme(arrayOfDigits){
console.log(arrayOfDigits)
return arrayOfDigits.reduce((sum, n)=>{
if(n > 0 && n < 10){
return sum + n
}
if(n >= 10){
return sum + Array.from(String(n), Number).reduce((s,d) => d+s)
}
return sum
})
}
console.log(quersumme(a))

18 changes: 18 additions & 0 deletions meineLoesungen/fn-objects-callbacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 1
// gegeben
// function add(a, b){
// return a + b
// }
//
// Erstelle eine Funktion namens aPlusb welche einen Parameter p entgegen nimmt,
// und intern immer add mit a=20 aufruft, b soll mit p belegt werden.
// Gib das Ergebnis der Berechnung zurück.
// Versuche zwei Lösungs-Varianten zu finden (Hint: bind).



// 2
// Die gegebene Funktion add aus Aufgabe 1 soll geändert werden.
// Neu möchten wir, dass immer alle Argumente geloggt werden (bevor die Berechnung gemacht wird).
// Der Aufrufer kann selbst entscheiden was für einen Logger er verwenden möchte.
// z.B. console.log oder console.warn wären mögliche Logger.
55 changes: 55 additions & 0 deletions meineLoesungen/functional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
let zahlen = [5,6,7,8]

// 1
{
// Funktional
function sum(array){
// Startwert || vorangegangener Wert:sum
// Aktuelle Zahl im Array:val
return array.reduce((sum,val) =>sum + val, 0);
}
console.log(sum(zahlen))

// Imperativ
let summe = zahlen.reduce((sum, val) =>sum + val)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mit imperativ ist gemeint: keine funktionalen konzepte verwenden. sprich: mit for schleife arbeiten.

console.log(summe)
}

//2
{
//Funktional
function even(array){
// Current number in iteration:value
return array.map(value => value%2 === 0 )
}

console.log(even(zahlen))

// Imperativ
// Current number in iteration:v
let isEvenArr = zahlen.map(v => v%2 === 0)
console.log(isEvenArr)
}

// 3.1
{
/**
*
* @param arr Array das gefiltert werden soll
* @param fn Funktion die mitgegeben wird um zu prüfen ob Kriterium erfüllt wird
*/

function myFilter(arr,fn){
let ret = []
arr.forEach(v =>{
if(fn(v)) ret.push(v)
})
return ret
}

let filtered = myFilter(zahlen,(n)=>n>6)
console.log(filtered)
}



13 changes: 13 additions & 0 deletions meineLoesungen/modules/app.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 'use strict' // optional, weil im Browser via <script type="module" ...> geladen

// import named export
import {Logger} from './logger.module.mjs'
import {PI} from './logger.module.mjs'
// rename import
import {PI as pii} from './logger.module.mjs'

let l = new Logger(console.log)
l.warn('hi')
l.log('du')
l.log([pii, PI, Logger.PI])
console.log(document.getElementsByTagName('p'))
11 changes: 11 additions & 0 deletions meineLoesungen/modules/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Modules</title>
<script type="module" src="app.mjs"></script>
</head>
<body>
<p></p>
</body>
</html>
File renamed without changes.
40 changes: 40 additions & 0 deletions meineLoesungen/modules/modules-es5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

// Namespacing
// Datei1
let ProjectX = ProjectX || {}
ProjectX.TodoList = ProjectX.TodoList || {}
ProjectX.TodoList.items = [
{
text: 'Einkaufen',
done: false
}
]

// Datei2
let ProjectX = ProjectX || {}
ProjectX.TodoList = ProjectX.TodoList || {}
ProjectX.TodoList.create = function(text){
this.items.push({
text,
done: false
})
}
ProjectX.TodoList.create('Abwaschen')

// IIFE
// loose module augmentation pattern
let TodoList = (function(modul){
let items = []
modul.create = function(text){
items.push({ text: text, done: false})
}
modul.getItems = function(){
return items;
}
return modul

})(TodoList || {})

TodoList.create('Einkaufen')
TodoList
Loading