Javascript,获取NaN,不确定原因

时间:2018-02-18 03:11:18

标签: javascript

我的任务是使堆栈计算机不使用数组来实现堆栈。输入数字后,它会进入堆栈顶部。输入操作时,操作将操作前两个数字,并将结果推送到堆栈顶部。堆栈上的推送和弹出似乎有效,但是当我尝试执行操作时,我正在获取NaN。我还是比较新的编程,所以我不知道从哪里开始。

HTML:

<input type="numberbox" id="number" value="0" />
<input type="button" onclick="putonstack();" value="Push to Stack" />
<br/> Please select operand:
<select id="operand">
          <option value="+">+</option>
          <option value="-">-</option>
          <option value="*">*</option>
          <option value="/">/</option>
        </select>
<br/>
<input type="button" value="Calculate" onclick="calculate();" />
<div id="output1">

</div>
<div id="output" />

脚本:

function LinkedList() {

  this.head = null;
  this.tail = null;
  this.length = 0;
}

function Node(value) {
  this.data = value;
  this.next = null;
  this.prev = null;
  this.content = null;
}
LinkedList.prototype.nodeAt = function(position) {
  var currentNode = this.tail,
    length = this.length,
    count = 1,
    message = {
      failure: 'Failure: non-existent node in this list.'
    };

  if (length === 0 || position < 1 || position > length) {
    throw new Error(message.failure);
  }

  while (count < position) {
    currentNode = currentNode.previous;
    count++;
  }

  return currentNode;
};
LinkedList.prototype.pop = function(position) {
  var node = this.nodeAt(position);

  if (node.prev) {
    node.prev.next = node.next;
  } else {
    this.head = node.next;
  }

  if (node.next) {
    node.next.prev = node.prev;
  } else {
    this.tail = node.prev;
  }

  this.length--;


  return node;
};

LinkedList.prototype.push = function(_content) {
  var node = new Node();
  node.content = _content;

  if (this.head === null) {
    this.head = node;
    this.length = 1;
    return node;
  }

  if (this.tail === null) {
    this.tail = node;
    this.tail.prev = this.head;
    this.head.next = this.tail;
    this.length++;

    return node;
  }

  this.tail.next = node;
  node.prev = this.tail;
  this.tail = node;
  return node;
};
LinkedList.prototype.print = function() {

  if (this.head === null) return "Empty List";
  var string = "";
  var node = this.head;
  while (node !== null) {
    string += node.content + " ";
    node = node.next;
  }

  return string;
};
var aList = new LinkedList();

function putonstack() {

  var c = document.getElementById("number").value;
  aList.push(c);
  document.getElementById('output').innerHTML = " ";
  document.getElementById('output').innerHTML = aList.print();
}

function calculate() {
  var operand = document.getElementById("operand").value;

  try {

    if (aList.length < 2) {
      throw "There must be at least 2 numbers to perform operation";
    } else {
      var a = parseFloat(aList.tail.prev);
      var b = parseFloat(aList.tail);
      var answer = 0;

      for (var i = 1; i <= 2; i++) {
        aList.pop();
      }

      if (operand == "+") {
        answer = a + b;
        aList.push(answer);
        document.getElementById('output').innerHTML = " ";
        document.getElementById('output').innerHTML = aList.print();
      } else if (operand == "-") {
        answer = a - b;
        aList.push(answer);
        document.getElementById('output').innerHTML = " ";
        document.getElementById('output').innerHTML = aList.print();
      } else if (operand == "*") {
        answer = a * b;
        aList.push(answer);
        document.getElementById('output').innerHTML = " ";
        document.getElementById('output').innerHTML = aList.print();
      } else if (operand == "/") {
        answer = a / b;
        aList.push(answer);
        document.getElementById('output').innerHTML = " ";
        document.getElementById('output').innerHTML = aList.print();
      }
    }
  } catch (err) {
    document.getElementById("ouput").innerHTML = +err;
    return;
  }


}

感谢您提供任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

您正在尝试从对象执行parseFloat。你应该从内容属性。

错误的代码:

var a = parseFloat(aList.tail.prev);
var b = parseFloat(aList.tail);

正确的代码:

var a = parseFloat(aList.tail.prev.content);
var b = parseFloat(aList.tail.content);