使用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 的示例完全按照上面所述进行了尝试。我尝试查看是否缺少某种模块,但安装的节点是最新的稳定版本。
实际上在 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.');