[英]Why am I getting NaN for this variable?
Anyone know why the bigNumberB variable is returning Not a number? 有人知道为什么bigNumberB变量返回的不是数字吗? I'm new to java and messing around with functions and I can't see what I'm doing wrong here but I'm sure its something simple. 我是Java的新手,并且迷惑了函数,在这里看不到我做错了什么,但我确定它很简单。 Thanks, I am getting 24, 24, NaN 谢谢,我要24、24,NaN
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);
You aren't returning anything from mathEquation
您没有从mathEquation
返回任何mathEquation
function mathEquation(moja, mbili, tatu, tano) {
var value = moja * mbili * tatu * tano;
console.log(value);
return value; // <- return the value here
}
Without returning a value, you are trying to add 11 + 23 + undefined
在不返回值的情况下,您尝试添加11 + 23 + undefined
You're not returning the sum of value in mathEquation
. 您没有在mathEquation
返回值的总和。 By default a function with no explicit return value will return undefined
. 默认情况下,没有显式返回值的函数将返回undefined
。 So essentially you're doing this in inceptEquation
: 所以从本质inceptEquation
您是在inceptEquation
这样做的:
11 + 23 + undefined;
Adding undefined to any number will result in NaN
. 将undefined添加到任何数字将导致NaN
。
Instead just return the sum as so. 相反,只需返回总和即可。 There's no need for the value
if it's not being used anywhere else in the function: 如果未在函数中的其他任何地方使用该value
则不需要该value
:
function mathEquation(moja, mbili, tatu, tano) {
return moja * mbili * tatu * tano;
}
All you have to do is to change the console.log()
s to return()
s. 您要做的就是将console.log()
更改为return()
。 Here is the simple 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));
Hopefully this is what you were expecting! 希望这就是您所期望的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.