简体   繁体   English

有谁知道为什么我的代码,特别是 javascript 不会运行?

[英]does anyone know why my code, specifically the javascript wont run?

My code doesn't seem to be running and it says that line 15 of my javascript (ie for loop) has an unexpected token.我的代码似乎没有运行,它说我的 javascript(即 for 循环)的第 15 行有一个意外的标记。

<!DOCTYPE html>
<head>
    <title> Lab 7 Part 2 </title>
    <meta charset="UTF-8"/>
    <canvas id= "drawingSurface"
           style="border-style: solid"
           width= "500" height="500"></canvas>
    <script src ="lab7b.js"></script>
</head>
<body onload= "setup();">
    <h1> Cat Mover </h1>

<input id ="up" type ="button" value="Up" onclick="upHandler()" >

<input id ="down" type ="button" value="Down" onclick="downHandler()">


<input id ="left" type ="button" value="Left" onclick="leftHandler()">


<input id ="right" type ="button" value="Right" onclick="rightHandler()">


<input id ="reset" type ="button" value="Reset" onclick="resetHandler()">
<section id="output">Move The Cat!<section>
</body>

my javascript file which seems to not work so can someone help me please?我的 javascript 文件似乎不起作用,所以有人可以帮助我吗?

var N, M, r;
var drawSurface;
var ctx;
var coordinates;

function setup(){
drawSurface= document.getElementById("drawingSurface");
ctx = drawSurface.getContext("2d");
ctx.translate(300,300);
}

function GenerateXY(M,N,r){
    coordinates = [[10,10],[[20,20]]

    for(var i = 0; i ,= M; i += 1;){
        var r2 = Math.abs(sin(angle*N/2));
        var x = r2*Math.sin(angle);
        var y = r2*Math.cos(angle);

        coordinates(i) = [x,y];

    }
    return coordinates;
}

function drawShape(){
    var numPetals = document.getElementById("text1").value;
    var numPoints = document.getElementById("text2").value;
    coordinates = getCoordinates(numPetals,NumPoints, 300);
    draw(coordinates);      
}

function draw(list){
    ctx.beginPath();
    for(var i = 0; i < list.length, i+= 1){
        ctx.lineTo(list[i][0], list[i][1]);
    }
    ctx.stroke();
}

If you see the problem can you point it out and let me know if there is anything wrong and why it is wrong如果你看到这个问题,你能指出它并让我知道是否有任何问题以及为什么它是错误的

[[10,10],[[20,20]] 
         ^^

remove a [删除一个 [

[[10,10],[20,20]]

You have two syntax errors in your top for loop.您的顶部 for 循环中有两个语法错误。 I guess the ,= should be <= .我猜,=应该是<= And you probably want to access coordinates at index i , which is done with coordinates[i] .并且您可能想要访问索引i处的coordinates ,这是通过coordinates[i]

for(var i = 0; i <= M; i += 1;){
    var r2 = Math.abs(sin(angle*N/2));
    var x = r2*Math.sin(angle);
    var y = r2*Math.cos(angle);

    coordinates[i] = [x,y];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM