0
我正在为函数RPNCalculator创建一个数组的方法,但由于某种原因它无法正常工作。减号操作总是返回正值
例如,当我尝试操作3-8时,它将返回5而不是-5,对于3 - 4,它将返回1而不是-1。正如你可以看到它里面的num变量。
我真的很感谢你的帮助。
RPN为[2,3,4]
RPNCalculator.prototype.minus = function() {
\t console.log("First item " + this[this.length - 2] + "\nLast Item " + this[this.length - 1]);
/* Logs:First item 3
Last Item 4 */
\t var num = this.pop(this[this.length - 2]) - this.pop(this[this.length - 1]);
\t console.log(num); // logs 1
\t this.push(num);
};
不知道你的实现细节,但也许你需要做var var = this.pop(this [this.length - 1]) - this.pop(this [this.length - 2]);'? – Mchl
请注意,您正在记录'this ['],但您正在减去'this.pop(this [...])'的*返回值*。那么'this.pop()'返回的是什么?它有什么作用?为什么在那里?似乎它可能会返回某种东西的“长度”。 –
正常情况下,['pop'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop)删除数组中的最后一项,返回该值并且不会没有任何争论。你的实现是否改变了? –