{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "d948a729-2d3e-4c5c-9407-82405bf0865e", "metadata": { "editable": false, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import { assertEquals } from \"jsr:@std/assert\"\n", "import \"https://git.amgdhg.de/kg/tslib/raw/branch/main/logger.ts\"" ] }, { "cell_type": "markdown", "id": "2cc36d84-90e4-41bd-a892-678c483ae88f", "metadata": { "editable": false, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Aufgabe 01.06.1\n", "Programmiere eine Funktion `getFirstElement`, die ein Array als Parameter bekommt und den ersten Eintrag dieses Arrays zurückgibt." ] }, { "cell_type": "code", "execution_count": null, "id": "b0ce656f-b119-4fe6-bfbf-bbb32a7ca176", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "function getFirstElement(a) {\n", " \n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "6855ea9a-b465-4c3f-b93b-9d4c9bba3aca", "metadata": { "editable": false, "jupyter": { "source_hidden": true }, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "let _nr = \"01.06.1\"\n", "Deno.test(`${_nr}: function`, () => {\n", " assertEquals(typeof getFirstElement, 'function')\n", "})\n", "Deno.test(`${_nr}: Parameter`, () => {\n", " assertEquals(getFirstElement.length, 1)\n", "})\n", "Deno.test(`${_nr}: getFirstElement([1, 2])=1`, () => {\n", " assertEquals(getFirstElement([1,2]), 1)\n", "})\n", "Deno.test(`${_nr}: getFirstElement(['Hänsel', 'Gretel'])='Hänsel'`, () => {\n", " assertEquals(getFirstElement(['Hänsel','Gretel']), 'Hänsel')\n", "})\n", "Deno.test(`${_nr}: getFirstElement(['a', 'b', 'c'])='a'`, () => {\n", " assertEquals(getFirstElement(['a','b','c']), 'a')\n", "})\n", "Deno.test(`${_nr}: getFirstElement([4, 2, 9, 4])=4`, () => {\n", " assertEquals(getFirstElement([4,2,9,4]), 4)\n", "})" ] }, { "cell_type": "markdown", "id": "4cb2f333-f1f1-475f-8fe8-a06302a31a62", "metadata": { "editable": false, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Aufgabe 01.06.2\n", "Programmiere eine Funktion `getLastElement`, die ein Array als Parameter bekommt und den letzten Eintrag dieses Arrays zurückgibt.\n", "### Tipp:\n", "Überlege dir zunächst, wie denn der Index des letzten Elementes ist." ] }, { "cell_type": "code", "execution_count": null, "id": "158385fd-be34-4f1d-9725-7cedee736958", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "58a9cc6d-683d-4f86-84b7-600d80d7b06d", "metadata": { "editable": false, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "let _nr = \"01.06.2\"\n", "Deno.test(`${_nr}: function`, () => {\n", " assertEquals(typeof getLastElement, 'function')\n", "})\n", "Deno.test(`${_nr}: Parameter`, () => {\n", " assertEquals(getLastElement.length, 1)\n", "})\n", "Deno.test(`${_nr}: getLastElement([1, 2])=2`, () => {\n", " assertEquals(getLastElement([1,2]), 2)\n", "})\n", "Deno.test(`${_nr}: getLastElement(['Hänsel', 'Gretel'])='Gretel'`, () => {\n", " assertEquals(getLastElement(['Hänsel','Gretel']), 'Gretel')\n", "})\n", "Deno.test(`${_nr}: getLastElement(['a', 'b', 'c'])='c'`, () => {\n", " assertEquals(getLastElement(['a','b','c']), 'c')\n", "})\n", "Deno.test(`${_nr}: getLastElement([4, 2, 9, 4])=4`, () => {\n", " assertEquals(getLastElement([4,2,9,4]), 4)\n", "})" ] }, { "cell_type": "markdown", "id": "3de65b2d-d74b-4eaa-b11e-beed0e658cd3", "metadata": { "editable": false, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Aufgabe 01.06.3 - Zufallszahlen\n", "Programmiere eine Methode `zufallsArray`, die einen Wert `laenge` als Parameter bekommt.\n", "\n", "Zurückgegeben werden soll ein Array, das genau `laenge` Einträge hat und mit Zufallszahlen gefüllt ist.\n", "\n", "### Hinweis:\n", "Zufallszahlen kann man mit `Math.random()` erzeugen.\n", "\n", "### Tipp:\n", "Beginne mit einem leeren Array `let arr = []` und füge anschließend mit einer Schleife und dem `push`-Befehl Einträge hinzu.\n", "\n", "### Tipp:\n", "Rufe die Funktion selbst mit `zufallsArray(10)` auch auf, damit du das Ergebnis auch direkt siehst!" ] }, { "cell_type": "code", "execution_count": null, "id": "367c5e53-49de-4dd0-ba10-723170affff2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "dfc977ed-abac-4c9f-9867-3227b2d869d6", "metadata": { "editable": false, "jupyter": { "source_hidden": true }, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "let _nr = \"01.06.3\"\n", "Deno.test(`${_nr}: function`, () => {\n", " assertEquals(typeof zufallsArray, 'function')\n", "})\n", "Deno.test(`${_nr}: Parameter`, () => {\n", " assertEquals(zufallsArray.length, 1)\n", "})\n", "Deno.test(`${_nr}: zufallsArray(0)=[]`, () => {\n", " assertEquals(zufallsArray(0), [])\n", "})\n", "Deno.test(`${_nr}: zufallsArray(1)`, () => {\n", " let last = 0\n", " for(let i = 0; i < 10; i++) {\n", " let now = zufallsArray(1)\n", " assertEquals(now.length, 1)\n", " assertEquals(now[0] >= 0, true)\n", " assertEquals(now[0] <= 1, true)\n", " assertEquals(now[0] === last, false)\n", " last = now[0]\n", " }\n", "})\n", "Deno.test(`${_nr}: zufallsArray(10)`, () => {\n", " let arr = zufallsArray(10)\n", " assertEquals(arr.length, 10)\n", " for (let i = 1; i < arr.length; i++) {\n", " assertEquals(arr[i] >= 0, true)\n", " assertEquals(arr[i] <= 1, true)\n", " if (i > 1) assertEquals(arr[i] === arr[i-1], false)\n", " }\n", "})\n", "Deno.test(`${_nr}: zufallsArray(1000)`, () => {\n", " let arr = zufallsArray(1000)\n", " assertEquals(arr.length, 1000)\n", " for (let i = 1; i < arr.length; i++) {\n", " assertEquals(arr[i] >= 0, true)\n", " assertEquals(arr[i] <= 1, true)\n", " if (i > 1) assertEquals(arr[i] === arr[i-1], false)\n", " }\n", "})" ] }, { "cell_type": "markdown", "id": "383f906a-4a8a-40d0-a5be-1231c3a787eb", "metadata": { "editable": false, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Aufgabe 01.06.4 - Summe\n", "Programmiere eine Funktion `summe`, die ein Array mit Zahlen als Parameter bekommt und die Summe aller Einträge berechnet und zurückgibt." ] }, { "cell_type": "code", "execution_count": null, "id": "30bef368-4084-4b96-877e-e58549ac4477", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "44dfc6a0-98b3-4c3c-a6b6-79c9b87efeab", "metadata": { "editable": false, "jupyter": { "source_hidden": true }, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "let _nr = \"01.06.4\"\n", "Deno.test(`${_nr}: function`, () => {\n", " assertEquals(typeof summe, 'function')\n", "})\n", "Deno.test(`${_nr}: Parameter`, () => {\n", " assertEquals(summe.length, 1)\n", "})\n", "Deno.test(`${_nr}: summe([])=0`, () => {\n", " assertEquals(summe([]), 0)\n", "})\n", "Deno.test(`${_nr}: summe([1])=1`, () => {\n", " assertEquals(summe([1]), 1)\n", "})\n", "Deno.test(`${_nr}: summe([1,2,3])=6`, () => {\n", " assertEquals(summe([1,2,3]), 6)\n", "})\n", "Deno.test(`${_nr}: summe([1.2, 3.4, 5.6, 7.8, 9.10])=27.1`, () => {\n", " assertEquals(summe([1.2, 3.4, 5.6, 7.8, 9.10]), 27.1)\n", "})" ] }, { "cell_type": "markdown", "id": "6800b795-bd76-407c-af6f-dc2d2fb9faaf", "metadata": { "editable": false, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Aufgabe 01.06.5 - Mittelwert\n", "Programmiere eine Funktion `mittelwert`, die vom als Parameter übergebenen Array den Mittelwert berechnet.\n", "\n", "### Tipp:\n", "Nutze die eben programmierte Methode `summe`" ] }, { "cell_type": "code", "execution_count": null, "id": "8ae1ddd8-6969-488c-9308-c9901bd868f7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "55e1afdf-9dc9-4328-8b31-cc2c122af64c", "metadata": { "editable": false, "jupyter": { "source_hidden": true }, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "let _nr = \"01.06.5\"\n", "Deno.test(`${_nr}: function`, () => {\n", " assertEquals(typeof mittelwert, 'function')\n", "})\n", "Deno.test(`${_nr}: Parameter`, () => {\n", " assertEquals(mittelwert.length, 1)\n", "})\n", "Deno.test(`${_nr}: mittelwert([0])=0`, () => {\n", " assertEquals(mittelwert([0]), 0)\n", "})\n", "Deno.test(`${_nr}: mittelwert([1])=1`, () => {\n", " assertEquals(mittelwert([1]), 1)\n", "})\n", "Deno.test(`${_nr}: mittelwert([1,2,3])=2`, () => {\n", " assertEquals(mittelwert([1,2,3]), 2)\n", "})\n", "Deno.test(`${_nr}: mittelwert([-2, -4, 17, 34])=11.25`, () => {\n", " assertEquals(mittelwert([-2, -4, 17, 34]), 11.25)\n", "})\n", "Deno.test(`${_nr}: mittelwert([1.2, 3.4, 5.6, 7.8, 9.10])=5.42`, () => {\n", " assertEquals(mittelwert([1.2, 3.4, 5.6, 7.8, 9.10]), 5.42)\n", "})" ] } ], "metadata": { "kernelspec": { "display_name": "Deno", "language": "typescript", "name": "deno" }, "language_info": { "codemirror_mode": "typescript", "file_extension": ".ts", "mimetype": "text/x.typescript", "name": "typescript", "nbconvert_exporter": "script", "pygments_lexer": "typescript", "version": "5.8.3" } }, "nbformat": 4, "nbformat_minor": 5 }