JavaScript计算器代码无法正常工作,不确定是什么问题

时间:2018-09-30 05:25:42

标签: javascript

所以我试着做一个计算器,但是它什至不起作用,这是我的代码:

void rightRotate(std::unique_ptr<Node>& y) {
    ....
    x->height = std::max(height(x->left), height(x->right)) + 1;
    y = std::move(x);
}

void leftRotate(std::unique_ptr<Node>& x) {
    ...
    y->height = std::max(height(y->left), height(y->right)) + 1;
    x = std::move(y);
}

`

我几乎在所有地方都进行了搜索,但是找不到错误,甚至尝试组装零件并分别进行测试。 我的猜测是该错误发生在执行部分或指数函数中。 很奇怪它最后没有显示。

2 个答案:

答案 0 :(得分:0)

while 1 > 0更改为while (1 > 0)

while的语法是
while (condition){ statement; }

答案 1 :(得分:0)

<html>
<head>

</head>
<body>
<h1>Calculator</h1>
<script>
            // Functions etc..

            function add(x, y) {
                var z = (parseInt(x) + parseInt(y));
                document.write("Result: " + z);
            }
            function subtract(x, y) {
                var z = (parseInt(x) - parseInt(y));
                document.write("Result: " + z);
            }
            function multiply(x, y) {
                var z = (parseInt(x) * parseInt(y));
                document.write("Result: " + z);
            }
            function exponention(x, power) {
                for(i = 1; i < power; i++) {
                    var z = (parseInt(x) * parseInt(x));
                }
                document.write("Result: " + z);
            }
            function divide(x, y) {
                var z = (Number(x) / Number(y));
                var zremain = (parseInt(x) % parseInt(y));
                document.write("Result: " + z);
                document.write("<br>");
                if (zremain > 0) {
                    document.write("Remainder: " + zremain);
                }
            }
        </script>

<script>
(function () {
 var result="";
   var choice = prompt('Choose an option:', '[{add}, {subtract}, {multiply}, {exponention} {divide}]');
    if (choice != null) {	
		if (choice == "add") {
                    var x = prompt("Enter your first number");
                    var y = prompt("Enter your second number");
                    add(x, y);
				   
                }
                else if (choice == "subtract") {
                    var x = prompt("Enter your first number");
                    var y = prompt("Enter your second number");
                    subtract(x, y);
					
                }
                else if (choice == "multiply") {
                    var x = prompt("Enter your first number");
                    var y = prompt("Enter your second number");
                    multiply(x, y);
					
                }
                else if (choice == "exponention") {
                    var x = prompt("Enter your first number");
                    var power = prompt("To the power of?");
                    exponention(x, power);
					
                }
                else if (choice == "divide") {
                    var x = prompt("Enter your first number");
                    var y = prompt("Enter your second number");
                    divide(x, y);
				   
                }
                else {
                    document.write("Invalid Input");
                }
    }
  
}());
</script>
</body>
</html>