How to use Array.map and Array.reduce() functions In Javascript
In this approach, an array consists the radius and height of a cone is mapped using the map() function, squaring the radius with Math.PI if it’s the first element. Then, the reduce() function multiplies the mapped values and divides by 3 to calculate the cone volume, with the result printed to the console.
Example: The below example uses Array.map and Array.reduce() functions to find the volume of Cone in JavaScript.
const rh = [5, 10];
const vol = rh.map((value, index) => index === 0 ?
Math.PI * value ** 2 : value).
reduce((acc, val) => acc * val) / 3;
console.log(vol);
Output
261.79938779914943
Find Volume of Cone using JavaScript
Mathematically, the formula for the Volume of the Cone is “volume = 1/3(pi * r * r * h)” where r is the radius of the circular base, and h is the height (the perpendicular distance from the base to the vertex). We can find this volume of cone using JavaScript by implementing various approaches along with practical implementation.
These are the following methods:
Table of Content
- Using Math.pow and Math.PI
- Using Array.map and Array.reduce() functions
- Using Template Literals and parseFloat() function
Contact Us