\n 应该缩进你的文本吗?

问题描述

我是一名 Python 程序员,在 Python 中,\n 会像这样呈现一个新行:

print("Hello \n World")
Hello
World

但我正在尝试使用以下代码块在 Javascript 中做到这一点:

if (userInput == wrongAnswer){
    let finalScore = initialScore + scoreForA;
    console.log(finalScore);
    if (finalScore == 5){
        console.log(rightScore);
    }
    else{
        console.log("Initial score:",initialScore,"\n",outputA,"Final score:",finalScore);
    }
}

编辑:顺便说一下,我已经定义了这些变量。

但它给了我:

enter image description here

我应该做到:

enter image description here

在 JavaScript 中换行后,\n 是否应该自动缩进错误答案和最终分数?

解决方法

请注意,您为 console.log 提供了多个参数,因此 Python 模拟将是 print("Hello\n","World"),它也会在第二行添加一个空格。多个参数总是用空格分隔,如果你不喜欢这样,连接它们或使用模板文字:

console.log(`Initial score: ${initialScore}\n${outputA}\nFinal score:${finalScore}`)
,

我认为这是因为函数 console.log() 在每个参数之间添加了一个空格。

enter image description here

你可以这样做:

console.log("Initial score" + 5 +"\n" + "WrongAnswer" +" :(" + "\n" + "Final score -1");

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...