JS Price Set - Volume of a Cube
EasyProgramming.net JavaScript practice set - get the volume of a cube with the math object!
by Nazmus Nasir
HTML
<!-- Easy JavaScript #9 - Calculate Volume of a Cube -->
<p>
How do you get the volume of a Cube? You multiply the length, width, and height.
</p>
<p>
Since it's a cube, you will only need to get one number.
</p>
<p>
One side of your cube has a measurement of <span id="length">___</span> units.
</p>
<p>
Your cube has a volume of <span id="answer">___</span> units cubed.
</p>
<p>
Visit <a href="http://www.easyprogramming.net">EasyProgramming.net</a> for more tutorials!
</p>
CSS
#length , #answer{
color: blue;
text-decoration: underline;
font-weight: bold;
}
JavaScript
/*
Formula for volume is LxWxH. Multiply the input by itself three times!
Or use the Math.pow method().
*/
//Lets grab the span ids and store them into variables for later use
var length = document.getElementById("length");
var answer = document.getElementById("answer");
//Ask the user for an input and store into a variable
var side = prompt("Enter the length/height/width of your cube", 2);
//calculate the volume - Method 1
//var volume = side * side * side;
//console.log("side: " + side + " volume: " + volume);
length.innerText = side;
//calculate the volume - Method 2
var volume = Math.pow(side,3);
answer.innerText = volume;