我刚刚开始我的学习过程,现在被困在这一根弦上。我搜索了 MDN、Google 和 Bing,但没有找到任何帮助。
我的代码说明告诉我分配一个变量,我照做了。然后它要我在 console.log 中进行测试。我已经这样做了,当我处理空格和标点符号时,它给了我一个错误,说它需要一个标识符,但却看到了“+”。
如果我去掉标点符号我没有错误,但没有标点符号。如果我去掉多余的空格和标点符号,我会得到一个奇怪的连续句子,但没有错误。我正在 Udacity 解决这个问题,它是第 2 课的第 24 个测验。
我的代码是:
var adjective1 = "amazing";
var adjective2 = "fun";
var adjective3 = "entertaining";
var madLib = "The intro to JavaScript course is " + adjective1. + " James and Julia are so " + adjective2. + " I cannot wait to work through the rest of this " + adjective3 + " content!";
console.log(madLib);
最佳答案
您还需要将点添加为字符串。
var adjective1 = "amazing";
var adjective2 = "fun";
var adjective3 = "entertaining";
var madLib = "The intro to JavaScript course is " + adjective1 + "."
+ " James and Julia are so " + adjective2 + "."
+ " I cannot wait to work through the rest of this " + adjective3 + " content!";
console.log(madLib);
点 .
在 Javascript 中有特殊的含义。它用作对象属性的访问器。
Math.floor(1.5); // return the integer value of the given number
在这里阅读更多关于 property accessor 的信息.
关于javascript - 连接字符串中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46549763/