[英]Im trying to make a lottoticket for school, but when i have to draw the numbers it doesnt work(line 59)
Everything but whats in line 59 seems to be working. 除了第59行中的所有内容外,其他所有内容似乎都在起作用。 Im very new to coding, and dont really get so... I couldnt find the mistake, i suspect, that im missing a {} or () but i dont know.
我对编码非常陌生,但实际上并没有得到……我找不到错误,我错过了{}或(),但我不知道。 I think the only mistake is in "TegnKupon", because thats the part google chrome tells me to change.
我认为唯一的错误是在“ TegnKupon”中,因为那是谷歌浏览器告诉我要更改的部分。
I tried talking about it with some mates, but they couldnt help either. 我试着和一些伙伴谈论这件事,但是他们俩也帮不上忙。 They made it but in another way, apparently.
他们做到了,但是显然是另一种方式。
<!DOCTYPE html>
<html>
<head>
<canvas id="myCanvas" width="600" height="300" style="border:1px solid #ff0000;">
</canvas>
</head>
<script type="text/javascript">
function find_randomtal(limit)
{
var random_tal
var cont = true
while (cont)
{
random_tal=Math.round(Math.random()*100+1);
if (random_tal <=limit)
{
cont = false
}
}
return random_tal;
}
var lottotal = [];
function Findlottotal ()
{
var min=1;
var random_talny;
var lottotal = [];
for (var j=min; j<=7; j++)
{
random_talny = find_randomtal(36);
if (lottotal.indexOf(random_talny)<0)
{
console.log(j + "random_talny: " + random_talny + " Findes ikke ");
lottotal[j] = random_talny
}
else
{
console.log (j + "random_talny: " + random_talny + " Findes ");
j--
}
}
}
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");
function TegnKupon()
{
var taeller = 0
for (var j=1;j<=4;j++)
{
for (var i=1;i<=9;i++)
{
taeller = taeller+1
xpos=i*100-50
ypos=100*j-50
if (lottotal.indexOf(taeller)<0)
{
ctx.beginPath();
ctx.fillStyle = "rgb(255,255,255)"
ctx.arc(xpos,ypos,50,0,2*Math.PI);
ctx.fillText("tallet",x,y+10)
}
else
{
ctx.beginPath();
ctx.fillStyle = "rgb(255,0,0)"
ctx.arc(xpos,ypos,50,0,2*Math.PI);
ctx.fillText("tallet",x,y+10)
}
}
}
}
lottotal = Findlottotal()
</script>
<body onload="Findlottotal();TegnKupon()">
</body>
</html>
A few things going on here... 这里发生了几件事...
First, x
and y
are undefined in TegnKupon
. 首先,
x
和y
在TegnKupon
中未定义。 Maybe you wanted to use xPos
and yPos
? 也许您想使用
xPos
和yPos
?
Next, I noticed that lottotal
is being assigned the value of Findlottotal
, but as it was, this function does not return anything. 接下来,我注意到为
lottotal
分配了Findlottotal
的值,但实际上,此函数不返回任何内容。 I've added a line at the bottom of Findlottotal
to return lottotal
. 我在
Findlottotal
的底部添加了一行以返回lottotal
。
Here's your snippet with my adjustments. 这是您对我进行调整的摘要。 It isn't giving console errors anymore.
它不再产生控制台错误。
<!DOCTYPE html>
<html>
<head>
<canvas id="myCanvas" width="600" height="300" style="border:1px solid #ff0000;">
</canvas>
</head>
<script type="text/javascript">
function find_randomtal(limit) {
var random_tal;
var cont = true;
while (cont) {
random_tal = Math.round(Math.random() * 100 + 1);
if (random_tal <= limit) {
cont = false;
}
}
return random_tal;
}
var lottotal = [];
function Findlottotal () {
var min = 1;
var random_talny;
var lottotal = [];
for (var j = min; j <= 7; j++) {
random_talny = find_randomtal(36);
if (lottotal.indexOf(random_talny) < 0) {
console.log(j + "random_talny: " + random_talny + " Findes ikke ");
lottotal[j] = random_talny;
}
else {
console.log (j + "random_talny: " + random_talny + " Findes ");
j--;
}
}
return lottotal;
}
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");
function TegnKupon()
{
var taeller = 0
for (var j = 1; j <= 4; j++)
{
for (var i = 1; i <= 9; i++)
{
taeller = taeller + 1;
xpos = i * 100 - 50
ypos = 100 * j - 50;
if (lottotal.indexOf(taeller) < 0) {
ctx.beginPath();
ctx.fillStyle = "rgb(255,255,255)";
ctx.arc(xpos, ypos, 50, 0, 2 * Math.PI);
ctx.fillText("tallet", xpos, ypos + 10);
}
else {
ctx.beginPath();
ctx.fillStyle = "rgb(255,0,0)"
ctx.arc(xpos,ypos,50,0,2*Math.PI);
ctx.fillText("tallet",xpos,ypos+10);
}
}
}
}
lottotal = Findlottotal()
</script>
<body onload="Findlottotal();TegnKupon()">
</body>
</html>
As the above to answers have shown, there was a few things I found wrong :) is this about what you're looking for? 如以上答案所示,我发现了一些错误:)这与您要查找的内容有关吗?
function find_randomtal(limit) { var random_tal var cont = true while (cont) { random_tal=Math.round(Math.random()*100+1); if (random_tal <=limit) { cont = false } } return random_tal; } function Findlottotal () { var min=0; var random_talny; var lottotal = []; for (var j=min; j<=7; j++) { random_talny = find_randomtal(36); if (lottotal.indexOf(random_talny)<0) { console.log(j + "random_talny: " + random_talny + " Findes ikke "); lottotal[j] = random_talny } else { console.log (j + "random_talny: " + random_talny + " Findes "); j-- } console.log(lottotal); } return lottotal; } var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext("2d"); function TegnKupon(lottotal) { var taeller = 0 for (var j=1;j<=4;j++) { for (var i=1;i<=9;i++) { taeller = taeller+1 xpos=i*100-50 ypos=100*j-50 console.log("taller " + lottotal); if (lottotal.indexOf(taeller)<0) { ctx.beginPath(); ctx.fillStyle = "rgb(255,255,255)" ctx.arc(xpos,ypos,50,0,2*Math.PI); ctx.fillText("tallet",xpos,ypos+10) } else { ctx.beginPath(); ctx.fillStyle = "rgb(255,0,0)" ctx.arc(xpos,ypos,50,0,2*Math.PI); ctx.fillText("tallet",xpos,ypos+10) } } } } document.getElementsByTagName("body")[0].onload = function() { var lottotal = Findlottotal(); TegnKupon(lottotal); };
<!DOCTYPE html> <html> <head> </head> <canvas id="myCanvas" width="600" height="300" style="border:1px solid #ff0000;"> </canvas> <body> </body> </html>
You shouldn't call your function that way. 您不应以这种方式调用函数。 Instead, wrap those into a function called init and call it onload
而是将它们包装到一个名为init的函数中,并将其称为onload
function init(){
Findlottotal();
TegnKupon();
}
window.onload = init();
Also you are referencing x
and y
without defining them. 另外,您引用
x
和y
未定义它们。 I think perhaps you meant to say xpos
and ypos
? 我想也许您是想说
xpos
和ypos
吗?
I'm not sure what output you're looking for, but I found 2 issues: 1. lottotal is not defined in function TengKupon 2. Also in TengKupon, in the if/else, ctx.filltext lists just "x" and "y". 我不确定您要查找的输出是什么,但发现了2个问题:1.在功能TengKupon中未定义lottotal 2.在TengKupon中,if / else中的ctx.filltext仅列出了“ x”和“ y”。 Did you mean to use "xpos" and "ypos" here?
您是要在此处使用“ xpos”和“ ypos”吗? When I replaced them accordingly, I got a grid with 'tallet' either once (from the "if") or multiple times (from the "else")
当我相应地替换它们时,我得到了一个带有“ tallet”的网格(一次(来自“ if”))或多次(来自“ else”)
So number 2 should be easy, but for number 1, I think the issue may be that you're not returning "lottotal" to make it available to TengKupon. 所以2号应该很容易,但是对于1号,我认为问题可能是您没有返回“ lottotal”以将其提供给TengKupon。 Your error is saying "can not read indexof of undefined, which is referring to lottotal.
您的错误是说“无法读取未定义的indexof,这是指乐透。
I assume you mean to return lottotal from FindLottotal, so I added "return lottotal" in the FindLottotal for loop, directly under the if else like so: 我假设您是要从FindLottotal返回乐透,所以我在FindLottotal for循环中的if循环正下方添加了“ return lottotal”:
var lottotal = [];
for (var j=min; j<=7; j++)
{
random_talny = find_randomtal(36);
if (lottotal.indexOf(random_talny)<0)
{
console.log(j + "random_talny: " + random_talny + " Findes ikke ");
lottotal[j] = random_talny
}
else
{
console.log (j + "random_talny: " + random_talny + " Findes ");
j--
}
return lottotal;
}
Without knowing more about what you're looking for, I can't provide any more info, but hopefully this will get you to the next step. 在不了解您要查找的内容的情况下,我无法提供更多信息,但是希望这可以使您进入下一步。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.