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

时间:2015-11-09 08:56:29

标签: javascript html arrays

我的代码似乎没有运行,它说我的javascript第15行(即for循环)有一个意外的令牌。

<!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>

我的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();
}

如果你看到问题可以指出来,让我知道是否有什么不对,为什么会出错?

2 个答案:

答案 0 :(得分:1)

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

删除[

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

答案 1 :(得分:0)

您的top for循环中有两个语法错误。我想,=应为<=。您可能希望在coordinates索引i访问coordinates[i]。{/ 1}}。

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];
}