Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
| gf_informatik:web:js [2025-11-01 11:00] – [Eingabe / Input] hof | gf_informatik:web:js [2025-11-01 11:03] (aktuell) – [JavaScript Basics] hof | ||
|---|---|---|---|
| Zeile 46: | Zeile 46: | ||
| while (i < 10) { | while (i < 10) { | ||
| console.log(i) | console.log(i) | ||
| + | i = i + 1 | ||
| } | } | ||
| Zeile 102: | Zeile 103: | ||
| <code javascript> | <code javascript> | ||
| // kann in JS über id direkte auf Element zugreifen: | // kann in JS über id direkte auf Element zugreifen: | ||
| - | spanNr.textContent = 41; | + | spanNr.textContent = 41 |
| </ | </ | ||
| Zeile 149: | Zeile 150: | ||
| ++++Tipps Würfel| | ++++Tipps Würfel| | ||
| - | * Mit `Math.floor(Math.random() * 6) + 1;` erzeugst du eine zufällige natürliche Zahl im Bereich von $1$ bis und mit $6$. | + | * Mit `Math.floor(Math.random() * 6) + 1` erzeugst du eine zufällige natürliche Zahl im Bereich von $1$ bis und mit $6$. |
| * Für Anzeige des Würfels: verwende HTML-Element **canvas**. Recherchiere online. | * Für Anzeige des Würfels: verwende HTML-Element **canvas**. Recherchiere online. | ||
| Zeile 208: | Zeile 209: | ||
| // SCHNAPS-BIER-SIRUP | // SCHNAPS-BIER-SIRUP | ||
| function handleKeyPressSBS(event) { | function handleKeyPressSBS(event) { | ||
| - | let inpAge = document.getElementById(" | + | let inpAge = document.getElementById(" |
| - | let drink = document.getElementById(' | + | let drink = document.getElementById(' |
| - | let age = inpAge.value; | + | let age = inpAge.value |
| if (age >= 18) { | if (age >= 18) { | ||
| - | drink.textContent = " | + | drink.textContent = " |
| } else if (age >= 16) { | } else if (age >= 16) { | ||
| - | drink.textContent = " | + | drink.textContent = " |
| } else { | } else { | ||
| - | drink.textContent = " | + | drink.textContent = " |
| } | } | ||
| } | } | ||
| // CLICKER | // CLICKER | ||
| - | let count = 0; | + | let count = 0 |
| function handleBtnClicker(event) { | function handleBtnClicker(event) { | ||
| - | count += 1; | + | count += 1 |
| - | let spanClick = document.getElementById(" | + | let spanClick = document.getElementById(" |
| - | spanClick.textContent = count; | + | spanClick.textContent = count |
| } | } | ||
| - | let funCount = 0; | + | let funCount = 0 |
| - | const funProb = 0.1; | + | const funProb = 0.1 |
| function handleBtnFunClicker(event) { | function handleBtnFunClicker(event) { | ||
| - | funCount = funCount + 1; | + | funCount = funCount + 1 |
| - | const randFloat = Math.random(); | + | const randFloat = Math.random() |
| if(randFloat < funProb){ | if(randFloat < funProb){ | ||
| - | funCount = 0; | + | funCount = 0 |
| } | } | ||
| - | spanFunClicker.textContent = funCount; | + | spanFunClicker.textContent = funCount |
| } | } | ||
| // DICE | // DICE | ||
| - | const l = 200; | + | const l = 200 |
| const diceImages = [ | const diceImages = [ | ||
| [[0.5*l, 0.5*l]], | [[0.5*l, 0.5*l]], | ||
| Zeile 254: | Zeile 255: | ||
| ] | ] | ||
| - | function handleBtnDice(event){ | + | function handleBtnDice(event) { |
| - | let randomInt = Math.floor(Math.random() * 6) + 1; | + | let randomInt = Math.floor(Math.random() * 6) + 1 |
| - | spanDice.textContent = randomInt; | + | spanDice.textContent = randomInt |
| // draw dice | // draw dice | ||
| - | const ctx = canvasDice.getContext(' | + | const ctx = canvasDice.getContext(' |
| - | canvasDice.width = l; | + | canvasDice.width = l |
| - | canvasDice.height = l; | + | canvasDice.height = l |
| | | ||
| - | ctx.clearRect(0, | + | ctx.clearRect(0, |
| // Draw the dice outline | // Draw the dice outline | ||
| - | ctx.strokeStyle = ' | + | ctx.strokeStyle = ' |
| - | // ctx.lineWidth = 5; | + | // ctx.lineWidth = 5 |
| - | const diceImage = diceImages[randomInt-1]; | + | const diceImage = diceImages[randomInt-1] |
| diceImage.forEach(point => { | diceImage.forEach(point => { | ||
| - | ctx.beginPath(); | + | ctx.beginPath() |
| - | ctx.arc(point[0], | + | ctx.arc(point[0], |
| - | ctx.fill(); | + | ctx.fill() |
| - | ctx.stroke(); // Outline the circle | + | ctx.stroke() // Outline the circle |
| - | ctx.closePath(); | + | ctx.closePath() |
| - | }); | + | }) |
| } | } | ||
| // CHAR COUNTER | // CHAR COUNTER | ||
| function handleInputTextArea(event) { | function handleInputTextArea(event) { | ||
| - | let txtArea = document.getElementById(" | + | let txtArea = document.getElementById(" |
| - | let spanBuchstaben = document.getElementById(" | + | let spanBuchstaben = document.getElementById(" |
| - | spanBuchstaben.textContent = txtArea.value.length; | + | spanBuchstaben.textContent = txtArea.value.length |
| } | } | ||
| // DATE | // DATE | ||
| function handleDateSelected(event) { | function handleDateSelected(event) { | ||
| - | const inpDate = document.getElementById(' | + | const inpDate = document.getElementById(' |
| - | const dateSelected = inpDate.value; | + | const dateSelected = inpDate.value |
| // get date of today | // get date of today | ||
| - | const today = new Date(); | + | const today = new Date() |
| - | const year = today.getFullYear(); // Get the current year (e.g., 2023) | + | const year = today.getFullYear() |
| - | const month = today.getMonth() + 1; // Get the current month (January is 0) | + | const month = today.getMonth() + 1 // Get the current month (January is 0) |
| - | const day = today.getDate(); // Get the current day of the month | + | const day = today.getDate() |
| - | const dateToday = `${year}-${month < 10 ? ' | + | |
| + | | ||
| // create date objects | // create date objects | ||
| - | const start = new Date(dateSelected); | + | const start = new Date(dateSelected) |
| - | const end = new Date(dateToday); | + | const end = new Date(dateToday) |
| // determine difference | // determine difference | ||
| - | const deltaMs = end - start; | + | const deltaMs = end - start |
| - | const deltaDays = Math.floor(deltaMs / (1000 * 60 * 60 * 24)); | + | const deltaDays = Math.floor(deltaMs / (1000 * 60 * 60 * 24)) |
| - | const passedDays = document.getElementById(' | + | const passedDays = document.getElementById(' |
| - | passedDays.textContent = deltaDays; | + | passedDays.textContent = deltaDays |
| } | } | ||
| </ | </ | ||