gpt4 book ai didi

javascript - 关于未定义数组的查询

转载 作者:行者123 更新时间:2023-11-29 23:12:13 25 4
gpt4 key购买 nike

我在修改代码的时候,发现自己写的代码不能正常运行,显示的字总是“undefined”,但是我检查了很多次,代码看起来很完美。你介意帮我看一下吗?这真的让我很困惑......非常感谢你。非常感谢你的帮助。

<!DOCTYPE html>
<html>
<head>
<style>
body{display:block;
margin:auto;
text-align:center;}
canvas{border:1px solid black;}
</style>
<script>
var canvas;
var ctx;
var timer;
var words=["canoe","buddha","elephant","dice"];//
var words=["canoe","buddha","elephant","dice"];
var answerIndex=-1;
var answer_x=-1;
var answer_y=-1;
var plate_x=-1;
var plate_y=-1;
var score=0;
function draw()
{ ctx.clearRect(0,0,canvas.width,canvas.height-10);
//canvas=document.getElementById("Canvas");
//ctx=canvas.getcontext("2d")
answer_x+=3;
answer_y+=3;

var answer=words[answerIndex];
ctx.font="20px Arial";
ctx.fillStyle="black";
ctx.fillText(answer,answer_x,answer_y);
var distance=answer_x-plate_x;
document.getElementById("plate_x").innerHTML=plate_x;
document.getElementById("word_x").innerHTML=answer_x;
document.getElementById("dist").innerHTML=distance;
if (answer_y>=plate_y)
{
clearInterval(timer);
if ((distance<50) && (distance>-50))
{
document.getElementById("message").innerHTML="Bravo!";
score++;
document.getElementById("score").innerHTML=score;
}
else
{
document.getElementById("message").innerHTML="Game over!";
}
}
}
function getRandomIndex()
{var random_number=Math.random*words.length;
var random_int=Math.floor(random_number);
return random_int;
}

function play()
{
canvas=document.getElementById("Canvas");
ctx=canvas.getContext("2d");
answerIndex = getRandomIndex();
var answer = words[answerIndex];
var imageFileName = answer + ".jpg";
document.getElementById("myPic").src = imageFileName;

answer_x=0;
answer_y=0;
ctx.clearRect(0,0,canvas.width,canvas.height);
plate_x=0;
plate_y=canvas.height-10;
ctx.fillStyle="blue";
ctx.fillRect(plate_x,plate_y,50,10);
clearInterval(timer);
timer=setInterval("draw()",100);
}


function moveleft()
{ ctx.clearRect(plate_x,plate_y,50,10);

if(plate_x>0)
{plate_x-=20;}

ctx.fillStyle="blue";
ctx.fillRect(plate_x,plate_y,50,10);

}
function moveright()
{ ctx.clearRect(plate_x,plate_y,50,10);

if(plate_x<(canvas.width-50))
{plate_x+=20;}

ctx.fillStyle="blue";
ctx.fillRect(plate_x,plate_y,50,10);

}
</script>
</head>
<body>
<h1>Catch the word!</h1>
<img id="myPic" alt="no pic" src="" width="200"/><br/>
<canvas id="Canvas" width="300" height="250"></canvas>
<br/><button onclick="play()">Play</button>
<button onclick="moveleft()">&larr;</button>
<button onclick="moveright()">&rarr;</button>
<p id="message">Move to catch the word!</p>
<p id="score"></p>
<p>Plate X-coordinate:</p><p id="plate_x"></p>
<p>Word X-coordinate:</p><p id="word_x"></p>
<p>Distance:</p><p id="dist"></p>
</body>
</html>

最佳答案

在您的 getRandomIndex() 函数中,您忘记了 Math.random 之后的括号,它将 random 作为属性而不是方法访问.因此,公式中的 Math.random 应该改为 Math.random()

您当前的代码不起作用,因为您的 getRandomIndex() 函数返回 NaN:

function getRandomIndex() {
var random_number = Math.random * words.length;
var random_int = Math.floor(random_number);

console.log(Math.random);
// ƒ random() { [native code] }

console.log(random_number);
// NaN

console.log(random_int);
// NaN

return random_int;
}

如果您将当前代码改为使用 Math.random(),则您的 getRandomIndex() 函数将返回您期望的随机整数值:

function getRandomIndex() {
var random_number = Math.random() * words.length; // changed code
var random_int = Math.floor(random_number);

console.log(Math.random());
// 0.40108128192401526 (of course this value will change each time)

console.log(random_number);
// 3.613675793700807 (of course this value will change each time)

console.log(random_int);
// 3 (of course this value will change each time)

return random_int;
}

关注来自 @David 的评论,将来如果您遇到类似这样的情况,您总是可以 console.log() 函数中的一些值,这些值未返回预期的输出。这将帮助您在控制台中没有错误时解决您的问题。

关于javascript - 关于未定义数组的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53684522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com