我试图从两个提示(这将需要一个字符串)解析一个整数,并将两个答案添加在一起。到目前为止,提示工作,但没有像预期的那样写入文档。你能告诉我为什么我的代码不能正确显示吗?试图解析提示中的整数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Chapter 2, Example 6</title>
</head>
<body>
<script>
var firstNumber = prompt("Enter the first number");
var secondNumber = prompt("Enter the second number");
var theTotal = firstNumber.parseInt() + secondNumber.parseInt();
document.write(firstNumber + "added to " + secondNumber + " equals" + theTotal);
</script>
</body>
</html>
字符串没有'.parseInt()'方法;正确使用['parseInt(firstNumber,10)'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt)。如果您打开浏览器的开发工具,控制台应该在该行上显示错误。你也应该测试结果是否是一个数字,因为用户可能会取消提示或键入字母而不是数字。 – nnnnnn
[Vue JS - Calculator - 将输出字符串读取为值]的可能重复(https://stackoverflow.com/questions/45237978/vue-js-calculator-read-output-string-as-value),但没有vueJS部分 –
你不会使用'parseInt(firstNumber)'吗? – Araymer