试图从提示中解析一个整数
Attempting to parse an integer from a prompt
我正在尝试从两个提示(将接受一个字符串)中解析一个整数并将两个答案加在一起。到目前为止,提示有效,但没有像预期的那样在文档中写入任何内容。你能告诉我为什么我的代码显示不正确吗?
<!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>
当您 运行 您的代码时,它必须显示 firstNumber.parseInt 不是函数 或 secondNumber.parseInt 不是函数function 因为我们使用 parseInt()
函数将例如 strings
转换为 integer
格式,所以它应该像 parstInt(firstNumber)
。在添加 firstNumber 和 secondNumber 之前还要检查 prompt
值,否则它将 return NaN
。
<!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","");
if (firstNumber == "" || secondNumber == "") {
var firstNumber = prompt("Enter the first number","");
var secondNumber = prompt("Enter the second number","");
}else{
var theTotal = parseInt(firstNumber, 10) + parseInt(secondNumber, 10);
document.write(firstNumber + " added to " + secondNumber + " equals " + theTotal);
}
</script>
</body>
</html>
window.prompt() returns 一个字符串。
parseInt() is not a method of the String prototype 而是一个接受要解析的字符串的内置函数。
更新这一行:
var theTotal = firstNumber.parseInt() + secondNumber.parseInt();
收件人:
var theTotal = parseInt(firstNumber) + parseInt(secondNumber);
此外,为了避免读者混淆并保证可预测的行为,请传递第二个参数(即 radix)- 假设以 10 为底的数字为 10。
var theTotal = parseInt(firstNumber, 10) + parseInt(secondNumber, 10);
var firstNumber = prompt("Enter the first number");
var secondNumber = prompt("Enter the second number");
var theTotal = parseInt(firstNumber, 10) + parseInt(secondNumber, 10);
console.log(firstNumber + " added to " + secondNumber + " equals: " + theTotal);
我正在尝试从两个提示(将接受一个字符串)中解析一个整数并将两个答案加在一起。到目前为止,提示有效,但没有像预期的那样在文档中写入任何内容。你能告诉我为什么我的代码显示不正确吗?
<!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>
当您 运行 您的代码时,它必须显示 firstNumber.parseInt 不是函数 或 secondNumber.parseInt 不是函数function 因为我们使用 parseInt()
函数将例如 strings
转换为 integer
格式,所以它应该像 parstInt(firstNumber)
。在添加 firstNumber 和 secondNumber 之前还要检查 prompt
值,否则它将 return NaN
。
<!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","");
if (firstNumber == "" || secondNumber == "") {
var firstNumber = prompt("Enter the first number","");
var secondNumber = prompt("Enter the second number","");
}else{
var theTotal = parseInt(firstNumber, 10) + parseInt(secondNumber, 10);
document.write(firstNumber + " added to " + secondNumber + " equals " + theTotal);
}
</script>
</body>
</html>
window.prompt() returns 一个字符串。
parseInt() is not a method of the String prototype 而是一个接受要解析的字符串的内置函数。
更新这一行:
var theTotal = firstNumber.parseInt() + secondNumber.parseInt();
收件人:
var theTotal = parseInt(firstNumber) + parseInt(secondNumber);
此外,为了避免读者混淆并保证可预测的行为,请传递第二个参数(即 radix)- 假设以 10 为底的数字为 10。
var theTotal = parseInt(firstNumber, 10) + parseInt(secondNumber, 10);
var firstNumber = prompt("Enter the first number");
var secondNumber = prompt("Enter the second number");
var theTotal = parseInt(firstNumber, 10) + parseInt(secondNumber, 10);
console.log(firstNumber + " added to " + secondNumber + " equals: " + theTotal);