简体   繁体   English

我试图为学校开一张彩票,但是当我不得不画数字时,它不起作用(第59行)

[英]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 . 首先, xyTegnKupon中未定义。 Maybe you wanted to use xPos and yPos ? 也许您想使用xPosyPos

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. 另外,您引用xy未定义它们。 I think perhaps you meant to say xpos and ypos ? 我想也许您是想说xposypos吗?

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.

相关问题 我一直在努力使这个球弹跳一天。 为什么不起作用? - I have been trying to make this ball bounce for a day. Why doesnt it work? 我试图在 if 语句中更改文本的值,但它不起作用我认为我使用了错误的逻辑 - im trying to change the value of a text inside an if statement but it doesnt work i think i use wrong logic 我在尝试制作的 discord.js bot 上有一些错误: - I have some errors on an discord.js bot im trying to make: 我有一个数组 = [1 , 2 , 3 , ... , 100] 并且我试图通过除以 3 的数字过滤我的数组。 我在做什么错? - i have an array = [1 , 2 , 3 , … , 100] and im trying to filter my array by numbers divide able by 3 . what i am doing wrong? 我试图建立一个网站,javascript 数学不起作用 - Im trying to make a website, javascript math doesn't work 我试图制作一个随机的单词生成器,为什么在我添加了一个将数字转换为文本的开关后,为什么它不会更新html? - im trying to make a random word generator, why wont it update html after i added a switch to convert numbers to text? 我试图用输入增加和减少数字来制作一个简单有趣的系统,但它不起作用 - I tried to make a simple fun system with increasing and decreasing numbers with input, but it doesnt work 我试图在 javascript 中制作数独游戏,但它不起作用:( - Im trying to make a sudoku puzzle in javascript but it wont work:( 我试图通过单击按钮来移动现有元素的位置来创建元素,但它不起作用 - Im trying to create element by clicking button with position of moving existing element but it doesnt work 我试图制作一个 discord 机器人,主要是为了好玩,但我似乎无法让我的 kick 命令中使用的 if else 语句正常工作 - Im trying to make a discord bot mostly just for fun but i cant seem to get the if else statements used in my kick command to work properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM