加载网站时,我没有收到提示,这是在 teamtreehouse 进行的练习,我看不到我做错了什么,有人可以向我解释一下吗?提前致谢
JavaScript
var firstName = prompt("Whats your first name?");
var lastName = prompt("whats your last name?");
var fullName = firstName.toUpperCase() + " " + lastName.toUpperCase();
var lengthOfFullName = fullName.length();
alert("The string" fullName "is" lengthOfFullName "long");
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Practice JavaScript Variables</title>
</head>
<body>
<script src="practice.js"></script>
</body>
</html>
最佳答案
.length
是一个属性,而不是一个函数,所以没有 括号。- 您需要使用
+
连接字符串。
使用精彩的代码片段功能,我们实际上可以运行它:
var firstName = prompt("Whats your first name?");
var lastName = prompt("whats your last name?");
var fullName = firstName.toUpperCase() + " " + lastName.toUpperCase();
var lengthOfFullName = fullName.length;
alert("The string " + fullName + " is " + lengthOfFullName + " long");
如果您使用的是 es6,则可以使用 string interpolation :
var firstName = prompt("What's your first name?");
var lastName = prompt("What's your last name?");
var fullName = `${firstName.toUpperCase()} ${lastName.toUpperCase()}`;
alert(`The string ${fullName} is ${fullName.length} long`);
或者,如果您想使用 es6 表达式插值真正疯狂:
var fullName = `${prompt("What's your first name?").toUpperCase()} ${prompt("What's your last name?").toUpperCase()}`;
alert(`The string ${fullName} is ${fullName.length} long`);
(我不建议您实际这样做,但这是插值整个表达式的有用演示。)
关于javascript - 为什么我的prompt()不能运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43668113/