|
| 1 | +/* |
| 2 | + author: PatOnTheBack |
| 3 | + license: GPL-3.0 or later |
| 4 | +
|
| 5 | + This script will find the volumes of various shapes. |
| 6 | +
|
| 7 | + More about volume: |
| 8 | + https://en.wikipedia.org/wiki/Volume |
| 9 | +*/ |
| 10 | + |
| 11 | +"use strict"; |
| 12 | + |
| 13 | +var PI = Math.PI; |
| 14 | + |
| 15 | +function vol_cube(side_length) { |
| 16 | + // Calculate the volume of a cube. |
| 17 | + // Cube the side_length. |
| 18 | + return Math.pow(side_length, 3); |
| 19 | +} |
| 20 | + |
| 21 | +function vol_cuboid(width, height, length) { |
| 22 | + // Calculate the Volume of a Cuboid." |
| 23 | + // Multiply lengths together. |
| 24 | + return width * height * length; |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +function vol_cone(area_of_base, height) { |
| 29 | + /* |
| 30 | + Calculate the Volume of a Cone. |
| 31 | +
|
| 32 | + Wikipedia reference: https: //en.wikipedia.org/wiki/Cone |
| 33 | + volume = (1 / 3) * area_of_base * height |
| 34 | + */ |
| 35 | + return (1 / 3) * area_of_base * height; |
| 36 | +} |
| 37 | + |
| 38 | +function vol_right_circ_cone(radius, height) { |
| 39 | + /* |
| 40 | + Calculate the Volume of a Right Circular Cone. |
| 41 | +
|
| 42 | + Wikipedia reference: https: //en.wikipedia.org/wiki/Cone |
| 43 | + volume = (1 / 3) * pi * radius ^ 2 * height |
| 44 | + */ |
| 45 | + |
| 46 | + return (1 / 3) * PI * Math.pow(radius, 2) * height; |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +function vol_prism(area_of_base, height) { |
| 51 | + /* |
| 52 | + Calculate the Volume of a Prism. |
| 53 | +
|
| 54 | + V = Bh |
| 55 | + Wikipedia reference: https: //en.wikipedia.org/wiki/Prism_(geometry) |
| 56 | + */ |
| 57 | + return area_of_base * height; |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +function vol_pyramid(area_of_base, height) { |
| 62 | + /* |
| 63 | + Calculate the Volume of a Prism. |
| 64 | +
|
| 65 | + V = (1 / 3) * Bh |
| 66 | + Wikipedia reference: https: //en.wikipedia.org/wiki/Pyramid_(geometry) |
| 67 | + */ |
| 68 | + return (1 / 3) * area_of_base * height; |
| 69 | +} |
| 70 | + |
| 71 | +function vol_sphere(radius) { |
| 72 | + /* |
| 73 | + Calculate the Volume of a Sphere. |
| 74 | +
|
| 75 | + V = (4 / 3) * pi * r ^ 3 |
| 76 | + Wikipedia reference: https: //en.wikipedia.org/wiki/Sphere |
| 77 | + */ |
| 78 | + return (4 / 3) * PI * Math.pow(radius, 3); |
| 79 | +} |
| 80 | + |
| 81 | +function vol_circular_cylinder(radius, height) { |
| 82 | + /* |
| 83 | + Calculate the Volume of a Circular Cylinder. |
| 84 | +
|
| 85 | + Wikipedia reference: https: //en.wikipedia.org/wiki/Cylinder |
| 86 | + volume = pi * radius ^ 2 * height |
| 87 | + */ |
| 88 | + return PI * Math.pow(radius, 2) * height; |
| 89 | +} |
| 90 | + |
| 91 | +// Log the Results of Various Volume Calculations. |
| 92 | +console.log("VOLUMES:"); |
| 93 | +console.log("Cube: " + vol_cube(2)); // = 8 |
| 94 | +console.log("Cuboid: " + vol_cuboid(2, 2, 2)); // = 8 |
| 95 | +console.log("Cone: " + vol_cone(2, 2)); // ~ = 1.33 |
| 96 | +console.log("Right Circular Cone: " + vol_right_circ_cone(2, 2)); // ~ = 8.38 |
| 97 | +console.log("Prism: " + vol_prism(2, 2)); // = 4 |
| 98 | +console.log("Pyramid: " + vol_pyramid(2, 2)); // ~ = 1.33 |
| 99 | +console.log("Sphere: " + vol_sphere(2)); // ~ = 33.5 |
| 100 | +console.log("Circular Cylinder: " + vol_circular_cylinder(2, 2)); // ~ = 25.1 |
0 commit comments