Javascript Calculation - NaN message



有人能帮我解决这个问题吗?当我尝试用 Javascript 进行计算时,它们似乎总是失败并返回为 NaN

function SpinRand()
{
    var a,b,c,d,e,f;
    a=Math.floor(Math.random()*9);
    b=Math.floor(Math.random()*9);
    c=Math.floor(Math.random()*9);
    d=Math.floor(Math.random()*9);
    e=Math.floor(Math.random()*9);
    f=a+b+c+d+e+f;
    alert(f);
}

当你这样做时

f=a+b+c+d+e+f;

你正在向f添加几个数字,这是undefined .这使得NaN.

你可能想要

f=a+b+c+d+e;

也许

return f;

最后也是。

请注意,使用循环可以更好地编写您的函数:

var f = 0;
for (var i=0; i<5; i++) f+=Math.floor(Math.random()*9); 

最新更新