【问题标题】:How to had space when concatenation a JavaScript sentence [duplicate]连接JavaScript句子时如何添加空格[重复]
【发布时间】:2020-01-17 22:17:50
【问题描述】:

我将特定单词插入到一个带有连接的句子中,但单词 a 一起运行,它们之间没有空格。

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);

【问题讨论】:

  • 只需在字符串中加一个空格即可:The Intro to JavaScript course is
  • 你的adjective变量都是NaN
  • 在字符串中包含空格
  • 或在形容词字符串中添加空格。
  • 考虑template literal: `The Intro to JavaScript course is ${adjective1} james and Julia are so...`

标签: javascript


【解决方案1】:

只需添加空格

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!';

你也不应该在变量赋值中的字符串之前有+。这将尝试将字符串转换为数字,并生成NaN,因为它们不是数字。

var adjective1 = 'amazing';
var adjective2 = 'fun';
var adjective3 = 'entertaining';

【讨论】:

    猜你喜欢
    • 2020-01-17
    • 2014-10-21
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 2017-05-01
    相关资源
    最近更新 更多