Ich muss zwei Zahlen in JavaScript multiplizieren, muss aber auf den Multiplikationsoperator "*" verzichten. Ist es möglich?
function a(b,c){
return b*c;
} // note:need to do this without the "*" operator
Lösung des Problems
Ja. Denn die Multiplikation ist nur eine mehrfache Addition. Verwenden Sie auch aussagekräftige Signaturen für Methoden, anstatt einzelne Alphabete zu verwenden.
function multiply(num, times){
// TODO what if times is zero
// TODO what if times is negative
var n = num;
for(var i = 1; i < times; i++)
num += n; // increments itself
return num;
}
Keine Kommentare:
Kommentar veröffentlichen