node.jslinuxvolumecalculation

Node.js at command promt is not completing calculations, how do I discover why?


Using Visual Studio as my editor, code is an example from Linux Professional Institute, so should be correct:

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.');

**When I attempt to run in Node.js environment through command prompt instead of completing the calculation and inserting it in the string for output, it outputs: **

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.

Thank you!

I am a beginner and I tried it exactly as stated above based on the Linux Professional Institute example. I have tried to see if there was some sort of module I was missing, but the node installed is the latest stable version.


Solution

  • According to you logic you want to use template literals.
    Actually for pass to console.log you've to use backtick(`) instead of single quotes(')

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

    OR

    Anothe solution is string concatenation (old fashioned):

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