Parameterdeklarationen in JavaScript
Parameter wurden schon behandelt. Diese Lektion hier ist eine Wiederholung und Vertiefung.
- main.js
"use strict";
function HalloMia(){ "use strict"; console.log( "Hallo Mia!" ); }
function HalloBen(){ "use strict"; console.log( "Hallo Ben!" ); }
function HalloLea(){ "use strict"; console.log( "Hallo Lea!" ); }
function main()
{ "use strict";HalloMia(); HalloBen(); HalloLea(); }
main();
- main.js
"use strict";
function Hallo( name ){ "use strict"; console.log( "Hallo " + name + "!" ); }
function main()
{ "use strict";Hallo( "Mia" ); Hallo( "Ben" ); Hallo( "Lea" ); }
main();
- main.js
"use strict";
function change( v ){ "use strict"; v = 5; console.log( v ); }
function main()
{ "use strict";let v = 0; change( v ); console.log( v ); }
main();
- Refaktor: Umbenennen des Parameters (alpha-Konversion)
- main.js
"use strict";
function Hallo( person ){ "use strict"; console.log( "Hallo " + person + "!" ); }
function main()
{ "use strict";Hallo( "Mia" ); Hallo( "Ben" ); Hallo( "Lea" ); }
main();
- Weiterleitung
"use strict";
function writeln( x ){ "use strict"; console.log( x ); }
function main()
{ "use strict";writeln( "alpha" ); }
main();
- 290 Zeichen (ohne Leerzeichen)
"use strict";
console.log( "Tomaten" );
console.log( "Rotkohl Gruenkohl" );
console.log( "Gurken" );
console.log( "Tomaten" );
console.log( "Rotkohl Gruenkohl" );
console.log( "Spinat" );
console.log( "Tomaten" );
console.log( "Rotkohl Gruenkohl" );
console.log( "Kohlrabi" );- Variante
"use strict";
function p( text )
{ "use strict"; console.log( text ); }function trg( word )
{ "use strict"; p( "Tomaten" ); p( "Rotkohl Gruenkohl" ); p( word ); }trg( "Gurken" ); trg( "Spinat" ); trg( "Kohlrabi" );
- verspielte Verkürzung
p=(x=>console.log(x))
t=(x=>{p("Tomaten");p("Rotkohl Gruenkohl");p(x);})
t("Gurken");t("Spinat");t("Kohlrabi");- Spionage-Anweisungen
- main.js
"use strict";
function g(){ "use strict"; document.write( "g" ); return 0; }
function f( a ){ "use strict"; document.write( "f" ); }
function main()
{ "use strict";f( g() ); console.log(); }
main();
- main.js
"use strict";
function u(){ "use strict"; document.write( "u" ); return 1; }
function v( a ){ "use strict"; document.write( "v" ); return 2; }
function w( a, b ){ "use strict"; document.write( "w" ); return a + b; }
function main()
{ "use strict";console.log( w( u(), v() )); }
main();
Aufruf einer übergebenen Funktion (callback )
- main.js
"use strict";
function call( f ){ return f(); }
function main()
{ "use strict";console.log( call( Math.random )); }
main();
Verwendung eines übergebenen Verzeichnisses
- main.js
"use strict";
function u( l ){ console.log( l.random() ); }
function main()
{ "use strict";u( Math ); }
main();
Übungsfragen
- main.js
"use strict";
function m( i ){ "use strict"; console.log( i ); }
function main()
{ "use strict";let i = 1;
let j = 2;
console.log( j ); }
main();
- main.js
"use strict";
function m1(){ "use strict"; console.log( "3" ); }
function m2(){ "use strict"; console.log( "4" ); }
function main()
{ "use strict";m1( m2() ); }
main();
Übungsaufgaben
- / = Zahl ausgeben
- Schreiben Sie eine Deklaration einer Funktion namens "schreibZahl", welche die Zahl, die ihr beim Aufruf als Argument übergeben wird, ausgibt!
- / = Summe ausgeben
- Schreiben Sie eine Deklaration einer Funktion namens "summeAusgeben", welche die Summe der beiden Zahlen, die ihr beim Aufruf als Argumente übergeben werden, ausgibt!
Zusatzaufgaben
- / + Verkürzung
- Schreiben Sie ein Programm, das die folgende Ausgabe auf die unten beschriebene Weise erzeugt.
Ausgabe
11910
11910 32251
11910
23141
23141 32251
23141
10312
10312 32251
10312
24170
24170 32251
24170
17233
17233 32251
17233
24947
24947 32251
24947- In der Ausgabe wird ein bestimmtes Muster mehrfach wiederholt. Das Muster wird aber nicht genau gleich wiederholt, sondern jedes Mal etwas verändert. Es gilt, dieses Muster zu erkennen und seine Ausgabe durch eine Funktionsdeklaration zu realisieren, in welcher das Veränderliche durch einen Parameter ausgespart ist. Alsdann soll die deklarierte Methode in »main« mehrfach aufgerufen werden, um die obige Ausgabe zu realisieren.
- / + Würfel
- Schreiben Sie eine Wertfunktion "wuerfel", die eine ganzzahlige Pseudozufallszahl ergibt, die größer-gleich 1 und kleiner-gleich dem Wert ihres ganzzahligen Arguments ist. (Die Funktion soll sich für Parameterwerte zwischen 1 und 32768 so verhalten.) So soll der Wert von "wuerfel( 6 )" beispielsweise eine ganzzahlige Pseudozufallszahl zwischen 1 und 6 sein.