2017-12-02 3 views
0

Wer weiß, warum die Variable bigNumberB zurückkehrt Keine Zahl? Ich bin neu in Java und rumalbern mit Funktionen und ich kann nicht sehen, was ich hier falsch mache, aber ich bin mir sicher, es ist etwas einfaches. Danke, ich bin immer 24, 24, NaNWarum bekomme ich NaN für diese Variable?

function mathEquation(moja, mbili, tatu, tano) { 
var value = moja * mbili * tatu * tano; 
console.log(value); 
} 

mathEquation(1, 2, 3, 4); 

function inceptEquation(sita, tisa) { 
var bigNumber = mathEquation(1, 2, 3, 4); 
console.log(bigNumber); 
var bigNumberB = sita + tisa + bigNumber; 
console.log(bigNumberB); 
} 

inceptEquation(11, 23); 
+1

'mathEquation' gibt einen Wert aus, gibt aber keinen Wert zurück. – Ryan

Antwort

0

Sie nichts Rückkehr aus mathEquation

function mathEquation(moja, mbili, tatu, tano) { 
    var value = moja * mbili * tatu * tano; 
    console.log(value); 
    return value; // <- return the value here 
} 

Ohne einen Wert zurückgibt, versuchen Sie 11 + 23 + undefined

0

Sie hinzuzufügen sind nicht die Summe von Wert in mathEquation. Standardmäßig gibt eine Funktion ohne expliziten Rückgabewert undefined zurück. So im Wesentlichen tun Sie dies in inceptEquation:

11 + 23 + undefined; 

auf eine beliebige Anzahl undefined Hinzufügen von in NaN führen.

Stattdessen einfach die Summe als so zurückgeben. Es gibt keine Notwendigkeit für die value wenn es nicht irgendwo anders in der Funktion verwendet:

function mathEquation(moja, mbili, tatu, tano) { 
    return moja * mbili * tatu * tano; 
} 
0

Alles, was Sie tun müssen, ist die console.log() s return() s zu ändern. Hier ist die einfache Version:

function multiplication(x, y, z, c) { 
    var value = x * y * z * c; 
    return(value); 
} 

console.log(multiplication(1, 2, 3, 4)); 

function addition(x, y) { 
    multiplication(1, 2, 3, 4); 
    return(x + y + multiplication(1, 2, 3, 4)); 
} 

console.log(addition(11, 23)); 

Hoffentlich ist das, was Sie erwartet haben!

Verwandte Themen