Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Nur mal als Lesehilfe: Currying

Was Du vorhast, sieht in der Umsetzung dann so aus:


slice=Function.prototype.call.bind([].slice);
function add(x){
var args=slice(arguments);
if(args.length>1) return args[0]+args[1];
return function(y){
return x+y;
}
}
[/php]

Hier das Fiddle: Edit fiddle - JSFiddle

Du musst in der ersten Funktion überprüfen, ob die Argumentliste mehr als ein Argument enthält. Wenn ja, dann nimm die ersten beiden und summiere sie; wenn nein, curry die zweite Funktion.

Verbesserung:

[php]
if(args.length>1) return args.reduce(function(o,n){return o+n;}, 0);

So kannst Du auch add mit beliebiger Parameteranzahl > 1 aufrufen.

Das ist eine Aufgabe vom Betrieb. Deswegen kann ich den Sinn nicht ganz erschließen.

Bin auch schon am tüffteln, wann man das gebrauchen könnte.

Das sind Higher Order Functions - funktionales Javascript, was ein bisschen über den 08/15 jQuery.on("click", bla) hinausgeht ;) Vielleicht haben die was vor mit Dir im Betrieb.

Bearbeitet von lilith2k3

  • Autor

Obwohl deine Lösung natürlich um einiges galanter ist.

Mein Code müsste doch so auch funktionieren oder ?

function add1(x,y){

if(!y){ return x;

return function(y){

return x+y;

};

}

}

return x+y;

}

Ich bin zwar kein Javascriptprofi aber:

Dein Code kann so nicht funktionieren.

Mal formatiert:


function add1(x,y)
{
if(!y)
{
return x;
return function(y) {
return x+y;
};
}
}
return x+y;
}
[/PHP]

Da ist nicht nur eine Klammer zuviel.

Da stehen zB zwei return Statements direkt hintereinander, es gibt also unerreichbaren Code.

Außerdem gibt es ein return Statement ausserhalb der Funktion.

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.