Skip to content

Commit ba0b6b7

Browse files
committed
Add 2 Math Algorithms
- Average (Median). - Volumes of various shapes.
1 parent 6660037 commit ba0b6b7

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

maths/average_median.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
author: PatOnTheBack
3+
license: GPL-3.0 or later
4+
5+
This script will find the average (median) of an array of numbers.
6+
7+
More about medians:
8+
https://en.wikipedia.org/wiki/Median
9+
*/
10+
11+
"use strict";
12+
13+
function median(nums) {
14+
// Find median of a list of numbers.
15+
// Sort array
16+
var sorted_list = nums.sort(function (a, b) {
17+
return a - b
18+
});
19+
var med;
20+
console.log("List of numbers: " + sorted_list);
21+
22+
// Is number of items in list even?
23+
if (sorted_list.length % 2 === 0) {
24+
// Find index for first middle value.
25+
var mid_index_1 = (sorted_list.length / 2) - 1;
26+
// Find index for second middle value.
27+
var mid_index_2 = (sorted_list.length / 2);
28+
// Find value that corresponds to `mid_index_2`
29+
var value_2 = sorted_list.slice(mid_index_2, -1)
30+
// Divide middle values by 2 to get average (mean).
31+
console.log("2: " + sorted_list[mid_index_2])
32+
med = ((sorted_list[mid_index_1]) + value_2[0]) / 2;
33+
return med; // Return makes `else:` unnecessary.
34+
}
35+
// Number of items is odd.
36+
var mid_index = (sorted_list.length - 1) / 2;
37+
// Middle index is median.
38+
med = sorted_list[mid_index];
39+
return med;
40+
}
41+
42+
// Run `median` Function to find average of two lists of numbers.
43+
console.log("Odd number of numbers:");
44+
console.log(median([2, 4, 6, 8, 20, 50, 70]));
45+
console.log("Even number of numbers:");
46+
console.log(median([2, 4, 6, 8, 20, 50]));

maths/volume.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)