如何单击或选择正确的javascript数组

如何单击或选择正确的javascript数组,javascript,arrays,Javascript,Arrays,我束手无策。首先让我开始,我对JavaScript完全是新手,我在自学 我试着问这些问题: 单击标题中的形状 然后显示一系列形状 如何显示形状数组并单击标题中的正确形状? 我还想存储它,并为每个正确选择的形状给它一个点 这是我的JSFIDLE: var MAX_WIDTH=100; var最大高度=100; //面向对象的JavaScript编写方法 变量形状=函数(宽度、高度、标题){ this.width=宽度, this.height=高度, this.title=标题; } Shap

我束手无策。首先让我开始,我对JavaScript完全是新手,我在自学

我试着问这些问题:

  • 单击标题中的形状
  • 然后显示一系列形状
如何显示形状数组并单击标题中的正确形状? 我还想存储它,并为每个正确选择的形状给它一个点

这是我的JSFIDLE:

var MAX_WIDTH=100;
var最大高度=100;
//面向对象的JavaScript编写方法
变量形状=函数(宽度、高度、标题){
this.width=宽度,
this.height=高度,
this.title=标题;
}
Shape.prototype.showtTitle=函数(){
警惕(“我是”+这个标题);
}
变量矩形=函数(宽度、高度){
//如果是宽度和高度相同的矩形,则返回一个正方形
如果(宽度===高度)返回新的方块(宽度);
//这使矩形成为形状的子对象
Shape.call(这个,宽度,高度,“矩形”),
}
变量平方=函数(维度){
//这使Square成为一个有形状的孩子
形状。称(这个,尺寸,尺寸,“正方形”);
}
//现在我们制作一定数量的形状
var shapes=新数组();
对于(变量i=0;i<5;i++){
var w=数学地板(数学随机()*最大宽度);
var h=数学地板(数学随机()*最大高度);
推(新矩形(w,h));
}
对于(var i=0;i

我想这就是你要找的东西。希望这有助于回答您的问题。

警报不会出现。我清理了代码,但仍然得到一个错误。未捕获的TypeError:undefined不是一个函数:console.log(shapes[i].showtTitle());我如何纠正这个错误,因为它没有执行我想要的伪代码。
var shapeName = ["Square", "Rectangle"];
// Random Shape to Select Title
var shapesTitle = shapeName[Math.floor(Math.random() * shapeName.length)];
document.getElementById("IdShapeTitle").innerHTML = shapesTitle.toString();


var varSquare = document.getElementById("square");
var varRect = document.getElementById("rect");


varSquare.onclick = function () {
    alert("I am square");
};


varRect.onclick = function () {
    alert("I am rectangle");
};

document.getElementById("currentName").innerHTML = shapesTitle.toString();
var MAX_WIDTH = 100;
var MAX_HEIGHT = 100;

// Object oriented way to write JavaScript
var Shape = function(width, height, title) {
    this.width = width,
    this.height = height,
    this.title = title;
}

Shape.prototype.showTitle = function() {
    alert("I am " + this.title);
}

var Rectangle = function(width, height) {
    // if it's a Rectangle with the same width and height return a Square
    if (width === height) return new Square(width);
    // this makes Rectangle a child of Shape
    Shape.call(this, width, height, "Rectangle"),
}

var Square = function(dimension) {
    // this makes Square a child of Shape
    Shape.call(this, dimension, dimension, "Square");
}


// now we make a set amount of Shapes
var shapes = new Array();
for (var i = 0; i < 5; i++) {
    var w = Math.floor(Math.random() * MAX_WIDTH);
    var h = Math.floor(Math.random() * MAX_HEIGHT);
    shapes.push(new Rectangle(w, h));
}

for (var i = 0; i < shapes.length; i++) {
    console.log(shapes[i].showTitle());
}