简体   繁体   中英

Is the \n supposed to indent your text?

I'm a python programmer and in Python, the \\n renders a new line like so:

print("Hello \n World")
Hello
World

But I'm trying to do that in Javascript with the following block of code:

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

Edit: By the way, I've defined these variables already.

But it gives me:

在此处输入图片说明

And I'm supposed to make it: 在此处输入图片说明

Is the \\n supposed to auto-indent Wrong answer and Final score after making a new line in JavaScript?

Note that you're providing multiple arguments to console.log , so the python analog would be print("Hello\\n", "World") which would add a space on the second line as well. Multiple arguments are always separated by a space, if you don't like that, concatenate them or use a template literal:

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

I think it is because the function console.log() adds a space between each params.

在此处输入图片说明

You can do that :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM