【问题标题】:Getting rid of a new line when using console.log in javascript在javascript中使用console.log时摆脱新行
【发布时间】:2021-07-25 18:04:57
【问题描述】:

我正在尝试在学习环境中运行此函数,但它会导致“您的程序的输出比预期的长”的错误,因为默认情况下,console.log 会生成一个换行符。有什么办法可以解决这个问题吗?

function calcSquare() {
    x = document.getElementsByTagName('input')[0].value;
    result = x**2
    console.log("The square of", parseInt(x), "is", parseInt(result))
}

【问题讨论】:

  • 这个具体是什么环境? console.log 的作用以及打印方式各不相同。
  • @Bergi 是 Viope
  • 嗯,我找不到任何关于它们如何执行代码的文档。我建议问问你的老师,或者 Viope 的支持。

标签: javascript function console output newline


【解决方案1】:

如果你在节点中,你可以使用process.stdout.write("foo");。您可以多次调用它而无需换行。

process.stdout.write('foo');
process.stdout.write('bar');

输出: foobar

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 2010-09-09
    • 2022-11-19
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多