命令提示符处的 Node.js 未完成计算,我如何找出原因?

问题描述 投票:0回答:1

使用Visual Studio作为我的编辑器,代码是来自Linux Professional Institute的示例,所以应该是正确的:

const sphereVol = (radius) => {
    return 4/3 * Math.PI * radius
}

console.log('A sphere with radius 3 has a ${sphereVol(3)} volume.');
console.log('A sphere with radius 6 has a ${sphereVol(6)} volume.');

**当我尝试通过命令提示符在 Node.js 环境中运行而不是完成计算并将其插入到字符串中进行输出时,它输出: **

C:\Users\court\node-examples-LPI>node ./volumeCalculator.js
A sphere with a radius 3 has a ${sphereVol(3)} volume.
A sphere with radius 6 has a ${sphereVol(6)} volume.

谢谢!

我是初学者,我根据 Linux Professional Institute 的示例完全按照上面所述进行了尝试。我尝试查看是否缺少某种模块,但安装的节点是最新的稳定版本。

node.js linux volume calculation
1个回答
0
投票

实际上在 console.log 中你必须使用 backtick(`) 而不是 单引号(')

console.log(`A sphere with radius 3 has a ${sphereVol(3)} volume.`);
console.log(`A sphere with radius 6 has a ${sphereVol(6)} volume.`);

另一个解决方案是:

console.log('A sphere with radius 3 has a '+ sphereVol(3)+ ' volume.');
console.log('A sphere with radius 6 has a '+ sphereVol(6)+ ' volume.');
© www.soinside.com 2019 - 2024. All rights reserved.