Javascript 为什么我的代码有时返回未定义的
我试图显示两个不同的数字,但每次结果都是相同的,有时显示未定义的。有办法解决这个问题吗Javascript 为什么我的代码有时返回未定义的,javascript,html,innerhtml,getelementbyid,Javascript,Html,Innerhtml,Getelementbyid,我试图显示两个不同的数字,但每次结果都是相同的,有时显示未定义的。有办法解决这个问题吗 var usedCard=[0]; var columnB=Math.floor(Math.random()*16); 函数columnb(){ 如果(使用卡片包括(B列)){ columnB=Math.floor(Math.random()*16); 柱状体b(); } else{return columnB}; } document.getElementById(“B1”).innerHTML=colu
var usedCard=[0];
var columnB=Math.floor(Math.random()*16);
函数columnb(){
如果(使用卡片包括(B列)){
columnB=Math.floor(Math.random()*16);
柱状体b();
}
else{return columnB};
}
document.getElementById(“B1”).innerHTML=columnBB();
使用卡片推送(B列);
columnB=Math.floor(Math.random()*16)
document.getElementById(“B2”).innerHTML=columnBB()代码>
您正在使用递归。当函数调用自身时,它永远不会返回,因为只有在从未调用if分支的情况下,它才会调用else分支。除去else,然后简单地返回,你应该是好的
var usedCard=[0];
var columnB=Math.floor(Math.random()*16);
函数columnb(){
如果(使用卡片包括(B列)){
columnB=Math.floor(Math.random()*16);
柱状体b();
}
返回栏B;
}
document.getElementById(“B1”).innerHTML=columnBB();
使用卡片推送(B列);
columnB=Math.floor(Math.random()*16)
document.getElementById(“B2”).innerHTML=columnBB()代码>
多次运行代码段