4

我正在尝试创建一个函数,它将通过一个数组运行并将它的值收集到一个看起来像这样的字符串:'[1,2,3]'。在某些情况下,我还需要它根据给定的索引仅显示数组的一部分。例如:从索引 0 打印到索引 1 的数组 [1,2,0] 将如下所示:'[1,2]'。出于某种原因,我的函数根本不提供任何输出。这里是:

function Tower(size, isFull) {
    this.count = 0;
    this.tower = new Array(size);

    this.add = function(disk) {
        this.tower[this.count++] = disk;
    };

    if (isFull) {
        for (var i = 1; i <= size; i++) {
            this.add(i);
        };
    }

    this.canAdd = function(disk) {
        return this.count == 0 || this.tower[this.count - 1] > disk;
    };

    this.getTopDiskValue = function() {
        return (this.count > 0) ? this.tower[this.count - 1] : 0;
    };

    this.popTop = function() {
        return this.tower[--this.count];
    };

    this.isFull = function() {
        return this.count == this.tower.length;
    };

    this.printable = function() {
        var output = "[";
        for (var i = 0; i < this.count; i++) {
            output += "" + this.tower[i] + ',';
        }
        return output.substring(0, output.length() - 1) + (output.length() > 1 ? ']' : "");
    };
}

我希望 printable() 函数返回字符串,以便代码:

var tower = new Tower(3,true);
alert(tower.printable());

将弹出一个带有文本“[1,2,3]”的警告框。该对象是 Java 的翻译。顺便说一句,它在 java 中效果很好,我猜翻译并不完美。

4

3 回答 3

2

你做的事情太复杂了。

假设您有一个数组声明为

var array = [1, 2, 3];

你得到想要的字符串

return '['+array.join(',')+']';

You don't need pop or add functions, they're also native (and heavily optimized) :

var last = array.pop()
array.push(newItem);

Reference :

Note that all browsers offer a console, in which you'll find a detailed explanation of your errors. Have a look for example at the Chrome Developer Tools.

于 2012-10-01T16:49:46.817 回答
1

use the Array.join() method:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/join


this.printable = function() {
    var output = new Array();
    for (var i = 0; i < this.count; i++) {
        output.push(this.tower[i]);
    }
    return output.length == 0 
        ? "" 
        : "[" + output.join(",") + ']';
};

or if it's as simple as it looks:

this.printable = function() {
    return this.count == 0 
        ? "" 
        : "[" + this.tower.join(",") + ']';
};
于 2012-10-01T16:50:47.487 回答
1

JavaScript is not Java - you don't get the length of an array or string by calling its .length() method, but just by retrieving its .length property. The exception which is thrown when you try to invoke the number is what crashes your script and prevents the alert. This would work:

this.printable = function() {
    var output = "[";
    for (var i = 0; i < this.count; i++) {
        output += "" + this.tower[i] + ',';
    }
    return output.substring(0, output.length - 1) + (output.length > 1 ? ']' : "");
};

However, you just can use the native .join() method to concatenate the array's values. Also, you should add your methods on the prototype of your Tower objects:

Tower.prototype.printable = function() {
    if (this.count)
        return "[" + this.tower.slice(0, this.count).join(",") + "]";
    else
        return "";
};

Btw: Usually this method is named toString - not only for convenience, but also it would get used when a Tower object is casted to a string value.

于 2012-10-01T16:53:10.390 回答