Arrays
parent
cd70407b7d
commit
08c364b3e8
|
|
@ -5,7 +5,7 @@
|
|||
"execution_count": null,
|
||||
"id": "86976bb8-a009-4ccf-8df3-69ac302459e9",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"editable": true,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
"cell_type": "markdown",
|
||||
"id": "d4b638e5-98c7-4c03-a496-cb4e0316808b",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
|
|
@ -41,7 +42,6 @@
|
|||
"execution_count": null,
|
||||
"id": "b05082a3-b2fc-46d1-9f91-d418f1861b69",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
|
|
|
|||
|
|
@ -0,0 +1,240 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a8050b44-a23c-4dfa-b1fc-06c0f89a9814",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"# Arrays\n",
|
||||
"Hat man eine Vielzahl an (gleichen) Daten, so fällt es schwer, für alle Daten einzelne Variablen anzulegen. Hierzu kann man ein sogenanntes \"Array\" benutzen.\n",
|
||||
"\n",
|
||||
"Ein Array ist ein Objekt, in dem mehrere Werte gespeichert werden können. Man kann es z.B. als \"Liste\" auffassen.\n",
|
||||
"\n",
|
||||
"## Achtung: Unterschiede JavaScript <> TypeScript <> andere Programmiersprachen\n",
|
||||
"### Unterschied 1: Typisierung\n",
|
||||
"\"Klassische\" Programmiersprachen wie Java, C++, C# lassen es nicht zu, dass man in einem Array unterschiedliche Daten speichert und verlangen, dass alle Elemente des Arrays vom gleichen Datentyp sind.\n",
|
||||
"\n",
|
||||
"Da JavaScript nicht streng typisiert ist, fällt diese Einschränkung weg. Nutzt man hingegen die Typisierung mittels TypeScript, so gibt es auch hier diese Einschränkung!\n",
|
||||
"\n",
|
||||
"**Wir werden deshalb vorrangig in einem Array die selben Datentypen speichern, also entweder nur Zahlen oder nur Texte oder nur...**\n",
|
||||
"\n",
|
||||
"### Unterschied 2: Erweiterbarkeit\n",
|
||||
"\"Klassische\" Programmiersprachen haben außerdem die Einschränkung, dass einmal angelegte Arrays weder vergrößert noch verkleinert werden können, also immer gleich viele Speicherplätze enthält. JavaScript/TypeScript/Python lassen dies zu. Wo hierbei die Vor- und Nachteile liegen werden wir im Rahmen des späteren Themas \"Abstrakte Datentypen\" uns näher anschauen"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5c142d44-d44f-4d17-a370-a26c3729988a",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"## Aufbau eines Arrays\n",
|
||||
"Ein Array kann auch als \"Schubladenschrank\" gesehen werden, bei dem in jeder Schublade eine Variable liegt:\n",
|
||||
"\n",
|
||||
"| Index | Wert |\n",
|
||||
"| :---: | :---: |\n",
|
||||
"| [0] | 5 |\n",
|
||||
"| [1] | 9 |\n",
|
||||
"| [2] | 14 |\n",
|
||||
"| [3] | -7 |\n",
|
||||
"| ... | ... |\n",
|
||||
"\n",
|
||||
"Zu beachten ist dabei dass die Zählung (der sogenannte \"Index\") der Schubladen/Felder/Einträge bei 0 beginnt."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "de4692c5-76fd-4559-84a8-5037c5ef8243",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"# Arbeiten mit einem Array\n",
|
||||
"Ein Array wird wie eine normale Variable angelegt, jedoch kommen die Werte in eckige Klammern. Hier wird z.B. das Array mit dem Namen `arr` angelegt mit den 4 Einträgen `5,9,14,-7`:\n",
|
||||
"```\n",
|
||||
"let arr = [5,9,14,-7]\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"*Anmerkung: am Mac werden eckige Klammern mit der Tastenkombination **[option]+[5]** bzw. **[option]+[6]** eingegeben.*\n",
|
||||
"\n",
|
||||
"Wollen wir auf einen einzelnen Eintrag zugreifen, so können wir an den Namen in eckigen Klammern den Index anhängen:\n",
|
||||
"```\n",
|
||||
"console.log( arr[2] ) // gibt \"14\" auf der Konsole aus\n",
|
||||
"arr[1] = 0 // ändert den zweiten Eintrag (mit Index 1) auf den Wert \"0\"\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Praktischer als in anderen Programmiersprachen ist, dass Arrays im Gesamten auf der Konsole ausgegeben werden können:\n",
|
||||
"```\n",
|
||||
"console.log( arr ) // gibt alle Inhalte des Arrays aus\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Wie oben schon angemerkt, können wir bei JavaScript/TypeScript auch einfach Elemente an ein Array anhängen. Dazu verwenden wir den Befehl `push`:\n",
|
||||
"```\n",
|
||||
"arr.push(42) // fügt an das Array \"arr\" ein neues Element mit dem Wert \"42\" an\n",
|
||||
"```\n",
|
||||
"*(Vor dem Punkt steht natürlich der jeweilige Name unseres Arrays)*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "a06dacf8-084f-472a-9983-25403008c7ea",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[ 5, 9, 14, -7 ]\n",
|
||||
"14\n",
|
||||
"[ 5, 0, 14, -7 ]\n",
|
||||
"[ 5, 0, 14, -7, 42 ]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"let arr = [5,9,14,-7]\n",
|
||||
"console.log( arr )\n",
|
||||
"console.log( arr[2] )\n",
|
||||
"arr[1] = 0\n",
|
||||
"console.log( arr )\n",
|
||||
"arr.push(42)\n",
|
||||
"console.log( arr )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4ac76977-797e-4ba4-ab32-dcb3d503a519",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"## Rückgabe eines Arrays aus einer Funktion\n",
|
||||
"Natürlich können wir so auch ein Array aus einer Funktion zurückgeben:\n",
|
||||
"```\n",
|
||||
"function toArray(a, b) {\n",
|
||||
" return [a, b]\n",
|
||||
"}\n",
|
||||
"```\n",
|
||||
"Die Funktion übernimmt 2 Parameter und gibt diese als Array wieder zurück."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "857fdf86-6321-4cb0-a0d7-c625e291239f",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[ \u001b[33m6\u001b[39m, \u001b[33m17\u001b[39m ]"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"function toArray(a, b) {\n",
|
||||
" return [a, b]\n",
|
||||
"}\n",
|
||||
"toArray(6, 17)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7daedfbf-a309-4307-954e-c1102662b643",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"## Länge eines Arrays\n",
|
||||
"Mit `length` kann man auf die Länge eines Arrays zugreifen:\n",
|
||||
"```\n",
|
||||
"console.log( arr.length ) // schreibt die Länge des Arrays \"arr\" auf die Konsole\n",
|
||||
"```\n",
|
||||
"*(Vor dem Punkt steht natürlich auch hier wieder der jeweilige Name unseres Arrays)*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "7c9daad5-3c31-4716-8aa5-5ef080b28df8",
|
||||
"metadata": {
|
||||
"editable": false,
|
||||
"slideshow": {
|
||||
"slide_type": ""
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[ 5, 0, 14, -7, 42 ]\n",
|
||||
"5\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"console.log( arr )\n",
|
||||
"console.log( arr.length )"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
|
|
@ -0,0 +1,412 @@
|
|||
{
|
||||
"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,
|
||||
"jupyter": {
|
||||
"source_hidden": true
|
||||
},
|
||||
"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
|
||||
}
|
||||
Loading…
Reference in New Issue