diff --git a/javascript/Javascript Expert/chapter09/index.html b/javascript/Javascript Expert/chapter09/index.html index 0d61c5c..fc4df7e 100644 --- a/javascript/Javascript Expert/chapter09/index.html +++ b/javascript/Javascript Expert/chapter09/index.html @@ -5,7 +5,9 @@ Destructuring Objects - + - + +

h1 element

+ diff --git a/javascript/Javascript Expert/chapter09/maps.js b/javascript/Javascript Expert/chapter09/maps.js new file mode 100644 index 0000000..4d5f2e9 --- /dev/null +++ b/javascript/Javascript Expert/chapter09/maps.js @@ -0,0 +1,32 @@ +const rest = new Map(); +rest.set("categories", ["Italian", "Pizzeria", "Vegetarian", "Organic"]); +rest.set("open", 11); +rest.set("close", 23); +rest.set(true, "We are open :)"); +rest.set(false, "We are closed :("); + +console.log(rest.get("name")); +console.log(rest.get(true)); +console.log(rest.get(1)); + +const time = 13; +console.log(rest.get(time > rest.get("open") && time < rest.get("close"))); +console.log(rest.has("categories")); +console.log(rest); +rest.delete(false); +console.log(rest); +console.log(rest.size); +rest.clear(); +// Das Feld [1,2] ist nicht glech dem Feld [1,2] aus dem Object +rest.set([1, 2], "Test"); +console.log(rest.get([1, 2])); + +// muss es so deklariert werden +const array = [1, 2]; +rest.set(array, "Test"); + +console.log(rest.get(array)); + +// Eine Eigenschaft des Maps DSs, kann auch ein HTML-Element sein +rest.set(document.querySelector("h1"), "HTML Element"); +console.log(rest); diff --git a/javascript/Javascript Expert/chapter09/mapsIteration.js b/javascript/Javascript Expert/chapter09/mapsIteration.js new file mode 100644 index 0000000..bc57e88 --- /dev/null +++ b/javascript/Javascript Expert/chapter09/mapsIteration.js @@ -0,0 +1,70 @@ +const question = new Map([ + ["question", "What is the best programming language in the world?"], + [1, "C"], + [2, "Java"], + [3, "Javascript"], + ["correct", 3], + [true, "Correct"], + [false, "Incorrect"], +]); +console.log(question.get(1)); +console.log(question.get(2)); +console.log(Object.entries(question)); + +// Konvertierung des Objects ins Map +const weekdays = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]; + +const openingHours = { + [weekdays[3]]: { + open: 12, + close: 22, + }, + [weekdays[4]]: { + open: 11, + close: 23, + }, + [weekdays[5]]: { + open: 0, // Open 24 hours + close: 24, + }, +}; + +const restaurant = { + name: "Classico Italiano", + location: "Via Angelo Tavanti 23, Firenze, Italy", + categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"], + starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"], + mainMenu: ["Pizza", "Pasta", "Risotto"], + order: function (starterIndex, mainIndex) { + return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]]; + }, + // man kann ein Objekt außerhalb eines anderen Objectes definieren, und es nachträglich bei einem anderen Objekt verwenden + openingHours, + orderDelivery: function ({ + starterIndex = 1, + mainIndex = 0, + time = "20:00", + address, + }) { + console.log( + `Order recieved! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}` + ); + }, +}; + +const hoursMap = new Map(Object.entries(openingHours)); + +console.log(question.get("question")); +for (const [key, value] of question) { + if (typeof key == "number") console.log(`Answer: ${key}: ${value}`); +} + +const answer = Number(prompt("Your answer")); + +console.log(question.get(question.get("correct") === answer)); +// aus dem Map DS einen Array erstellen +console.log([...question]); + +console.log([...question.values()]); +console.log([...question.keys()]); +console.log([...question.entries()]); diff --git a/javascript/Javascript Expert/chapter09/strings.js b/javascript/Javascript Expert/chapter09/strings.js new file mode 100644 index 0000000..b1d61e7 --- /dev/null +++ b/javascript/Javascript Expert/chapter09/strings.js @@ -0,0 +1,145 @@ +const airline = "TAB Air Portugal"; +const plane = "A320"; + +console.log(plane[0]); +console.log(plane[1]); +console.log(plane[2]); +console.log(plane[3]); + +console.log(plane.length); +console.log("B737"[0]); +console.log(airline.indexOf("r")); +console.log(airline.lastIndexOf("r")); +console.log(airline.slice(4, 7)); + +console.log(airline.slice(0, airline.indexOf(" "))); +console.log(airline.slice(airline.lastIndexOf(" ") + 1)); +console.log(airline.slice(-2)); +console.log(airline.slice(1, -1)); + +const checkMiddleSeat = function (seat) { + // B und E sind in der Mitte des Flugzeuges + const s = seat.slice(-1); + if (s === "B" || s === "E") { + console.log("Sie haben einen Sitzplatz in der Mitte des Flugzeugs 😀"); + } else { + console.log("Sie haben glück 😎"); + } +}; + +checkMiddleSeat("16B"); +checkMiddleSeat("16C"); +checkMiddleSeat("16A"); +checkMiddleSeat("16D"); +console.log(airline); +console.log(airline.toLowerCase()); +console.log(airline.toUpperCase()); + +const passenger = "DaViD"; +const passengeLower = passenger.toLowerCase(); +const passengerCorrect = + passengeLower[0].toUpperCase() + passengeLower.slice(1); +console.log(passengerCorrect); + +// +const correctName = function (passenger) { + return passenger[0].toUpperCase() + passenger.slice(1).toLowerCase(); +}; + +console.log(correctName("NIkA")); +console.log(correctName("jOaNnE")); +console.log(correctName("DAViD")); + +// Emailvergleich +const email = "davidaster@gmail.com"; +const loginEmail = " davidaster@gmail.com\n"; + +const lowerLoginEmail = loginEmail.toLowerCase(); +const trimmedEmail = lowerLoginEmail.trim(); +console.log(trimmedEmail); + +const normalizedEmail = loginEmail.toLowerCase().trim(); + +console.log(normalizedEmail); +const checkEmail = function (email, loginEmail) { + return email === loginEmail; +}; +console.log(checkEmail(email, normalizedEmail)); + +// ersetzen +const priceGB = "298,25£"; +const priceEU = priceGB.replace("£", "€").replace(",", "."); +console.log(priceEU); + +const announcement = + "All passengers come to boarding door 23. Boarding door 23"; +console.log(announcement.replace("door", "gate")); +// console.log(announcement.replaceAll("door", "gate")); +// ES 2020 hat noch keine Möglichkeit eine replaceAll Methode zu benutzen, deshalb wervendet man die Regular Expression für ersetzen aller der entsprechenden Wörter +console.log(announcement.replace(/door/g, "gate")); + +// Booleans +const planeNeo = "Airbus A320neo"; +console.log(planeNeo.includes("A320")); +console.log(planeNeo.includes("Boeing")); +console.log(planeNeo.startsWith("Air")); + +if (planeNeo.startsWith("Airbus") && planeNeo.endsWith("neo")) { + console.log("Part of the new Airbus family"); +} + +const checkBaggage = function (items) { + const i = items.toLowerCase(); + i.includes("knife" || i.includes("gun")) + ? console.log("You are not allowed on board") + : console.log("You are allowed on board"); +}; + +checkBaggage("I have a laptop, some food, and a pocket knife"); +checkBaggage("stocks and camera"); +checkBaggage("Got some snacks"); + +// ein String Mithlfe eine split() Metode auf Teile aufteilen +const str = "Das+ist+ein+schöner+Satz"; +const splitedStr = str.split("+"); +console.log(splitedStr); + +const [firstName, lastName] = "David Aster".split(" "); +const fullName = ["Herr", firstName, lastName.toUpperCase()].join(" "); +console.log(fullName); + +const capitalizeName = function (name) { + const names = name.split(" "); + const namesUpper = []; + for (const n of names) { + // namesUpper.push(n[0].toUpperCase() + n.slice(1).toLowerCase()); + namesUpper.push(n.replace(n[0], n[0].toUpperCase())); + } + console.log(namesUpper.join(" ")); +}; + +capitalizeName("van der koogh"); + +// Padding +const message = "Go to the gate 23"; +console.log(message.padStart(25, "*").padEnd(33, "*")); + +const maskCreditCard = function (number) { + const str = number + ""; + const last = str.slice(-4); + return last.padStart(str.length, "*"); +}; + +console.log(maskCreditCard(6574654)); +console.log(maskCreditCard(4645465645456456)); +console.log(maskCreditCard(87654684364643)); +// Repeat +const message2 = "Bad weather... All departures delayed..."; +console.log(message2.repeat(5)); + +const planesInLine = function (n) { + console.log(`The are ${n} planes in line ${"✈️".repeat(n)}`); +}; +planesInLine(3); +planesInLine(7); +planesInLine(10);