首页 > 解决方案 > 在javascript中使用console.log时摆脱新行

问题描述

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

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

标签: javascriptfunctionconsoleoutputnewline

解决方案


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

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

输出: foobar


推荐阅读