My goal is to draw lines based on the coordinates provided in the array called points. However, I encountered an error when calling the method. Oddly enough, when I try to access a specific element using console.log(points[1][1]), it works perfectly. Can someone help point out what I might be missing here?
"Uncaught TypeError: Cannot read property '0' of undefined"
Below is the code snippet:
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var points = [[50, 50], [50, 100], [25, 120],
[100, 50], [70, 90], [100, 90], [70, 120]];
function drawPoints(array) {
ctx.beginPath();
ctx.moveTo(array[0][0], array[0][1]);
for (var i = 1; i <= array.length; i++) {
ctx.lineTo(array[i][0], array[i][1]);
}
ctx.stroke();
}
drawPoints(points);