使用javascript计算时未定义
每次我运行这段代码(var horizontal speed是一个数字,由用户用一个表单插入),我都会得到单词undefined,后跟文本execute@e[type…等等。 我不希望输出中出现未定义的单词,因为它也会显示在文本区域中。我的代码中有什么错误导致该单词出现使用javascript计算时未定义,javascript,Javascript,每次我运行这段代码(var horizontal speed是一个数字,由用户用一个表单插入),我都会得到单词undefined,后跟文本execute@e[type…等等。 我不希望输出中出现未定义的单词,因为它也会显示在文本区域中。我的代码中有什么错误导致该单词出现 function command(){ var form = document.getElementById("form"); var
function command(){
var form = document.getElementById("form");
var horizontal_speed = form.elements["horizontal_speed"].value;
for (var i = 1; i <= horizontal_speed; i ++){
var standard_command = "execute @e[type=armor_stand,name=test] ~ ~ ~ /setblock etc particle insert_particle ~x_position ~1 ~z_position 0 0 0 0.01 1 force"
var rad = (2*i*Math.PI)/horizontal_speed;
var x_position = Math.round(Math.cos(rad) * 100) / 100;
var z_position = Math.round(Math.sin(rad) * 100) / 100;
var add_x_position = standard_command.replace('x_position', x_position);
var add_z_position = add_x_position.replace('z_position', z_position);
var output = output + add_z_position;
}
document.getElementById('output').value = output;
}
function命令(){
var form=document.getElementById(“表单”);
var水平速度=form.elements[“水平速度”].值;
对于(var i=1;i而言,问题在于:
var output = output + add_z_position;
在第一次传递时,输出
以前从未分配过值,因此它有未定义
将var
部分移到循环上方,并给它一个初始值,可能是”
:
可能是
output += add_z_position;
如果您愿意。问题在于:
var output = output + add_z_position;
在第一次传递时,输出
以前从未分配过值,因此它有未定义
将var
部分移到循环上方,并给它一个初始值,可能是”
:
可能是
output += add_z_position;
如果愿意。您必须初始化输出变量。您应该调试代码。您必须初始化输出变量。您应该调试代码。