From fb220e500b5594c9798b9bc0d597dd96867a2f42 Mon Sep 17 00:00:00 2001 From: adityatandon007 Date: Thu, 19 Oct 2017 09:05:58 +0530 Subject: [PATCH 1/2] adds javascript version for exiting formulas --- .../javascript/radians_and_degrees.js | 15 ++++++ geometry/triangles/javascript/Triangle.js | 19 +++++++ .../javascript/volume_formulas.js | 51 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 geometry/radians_degrees_conversion/javascript/radians_and_degrees.js create mode 100644 geometry/triangles/javascript/Triangle.js create mode 100644 geometry/volume_formulas/javascript/volume_formulas.js diff --git a/geometry/radians_degrees_conversion/javascript/radians_and_degrees.js b/geometry/radians_degrees_conversion/javascript/radians_and_degrees.js new file mode 100644 index 000000000..92984586c --- /dev/null +++ b/geometry/radians_degrees_conversion/javascript/radians_and_degrees.js @@ -0,0 +1,15 @@ +// ES5 way + +function radians_to_degrees(radians){ + return radians*(180/Math.PI); +} + +function degrees_to_radians(degrees){ + return degrees*(Math.PI/180); +} + +// ES6 way + +let radians_to_degrees = (radians) => radians*(180/Math.PI); + +let degrees_to_radians = (degrees) => degrees*(Math.PI/180); diff --git a/geometry/triangles/javascript/Triangle.js b/geometry/triangles/javascript/Triangle.js new file mode 100644 index 000000000..50a939dc2 --- /dev/null +++ b/geometry/triangles/javascript/Triangle.js @@ -0,0 +1,19 @@ +// ES5 way + +function area(base, height) { + return (base*height)/2; +} + +function heron(side1, side2, side3) { + var s = (side1+side2+side3)/2; + return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); +} + +// ES6 way + +let area = (base, height) => (base*height)/2; + +let heron = (side1, side2, side3) => { + let s = (side1+side2+side3)/2; + return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); +} \ No newline at end of file diff --git a/geometry/volume_formulas/javascript/volume_formulas.js b/geometry/volume_formulas/javascript/volume_formulas.js new file mode 100644 index 000000000..cdda5db8a --- /dev/null +++ b/geometry/volume_formulas/javascript/volume_formulas.js @@ -0,0 +1,51 @@ +// ES5 way + +function cube_volume(edge_length) { + return edge_length**3; +} + +function cuboid_volume(width, length, height) { + return width*length*height; +} + +function cylinder_volume(radius, height) { + return Math.PI*height*(radius**2); +} + +function sphere_volume(radius) { + return (4/3)*Math.PI*(radius**2); +} + +function cone_volume(radius, height) { + return (1/3)*Math.PI*height*(radius**2); +} + +function ellipsoid_volume(r1,r2,r3) { + return (4/3)*Math.PI*r1*r2*r3; +} + +function pyramid_volume(width, length, height) { + return (1/3)*width*length*height; +} + +function hemisphere_volume(radius) { + return (2/3)*Math.PI*(radius**2); +} + +// ES6 way + +let cube_volume = (edge_length) => edge_length**3; + +let cuboid_volume = (width, length, height) => width*length*height; + +let cylinder_volume = (radius, height) => Math.PI*height*(radius**2); + +let sphere_volume = (radius) => (4/3)*Math.PI*(radius**2); + +let cone_volume = (radius, height) => (1/3)*Math.PI*height*(radius**2); + +let ellipsoid_volume = (r1,r2,r3) => (4/3)*Math.PI*r1*r2*r3; + +let pyramid_volume = (width, length, height) => (1/3)*width*length*height; + +let hemisphere_volume = (radius) => (2/3)*Math.PI*(radius**2); From 112c3c518e3de0deee87584dbc6351d647bb2980 Mon Sep 17 00:00:00 2001 From: adityatandon007 Date: Fri, 20 Oct 2017 01:25:39 +0530 Subject: [PATCH 2/2] added more javascript version for existing function --- .../permutations/javascript/permutation.js | 23 ++++++++++++ .../javascript/quadratic_formula.js | 37 +++++++++++++++++++ .../javascript/sum_of_GP.js | 9 +++++ geometry/arc_angle/javascript/arc_angle.js | 9 +++++ 4 files changed, 78 insertions(+) create mode 100644 algebra/permutations/javascript/permutation.js create mode 100644 algebra/quadratic_formula/javascript/quadratic_formula.js create mode 100644 algebra/sum_of_n_terms_of_gp/javascript/sum_of_GP.js create mode 100644 geometry/arc_angle/javascript/arc_angle.js diff --git a/algebra/permutations/javascript/permutation.js b/algebra/permutations/javascript/permutation.js new file mode 100644 index 000000000..386727d61 --- /dev/null +++ b/algebra/permutations/javascript/permutation.js @@ -0,0 +1,23 @@ +// ES5 + +function factorial(n) { + if (n === 0) + return 1; + else + return n * factorial(n - 1); +} + +function permutation(n, r) { + return factorial(n) / factorial(n - r); +} + +// ES6 + +let factorial = (n) => { + if (n === 0) + return 1; + else + return n * factorial(n - 1); +}; + +let permutation = (n, r) => factorial(n) / factorial(n - r); diff --git a/algebra/quadratic_formula/javascript/quadratic_formula.js b/algebra/quadratic_formula/javascript/quadratic_formula.js new file mode 100644 index 000000000..d9bcf2132 --- /dev/null +++ b/algebra/quadratic_formula/javascript/quadratic_formula.js @@ -0,0 +1,37 @@ +// ES5 way + +function quadratic_formula(a, b, c) { + + var discrimiant = b**2 - (4 * a * c); + if (discrimiant < 0) { + return "This equation does not have a real solution"; + } + else if (discrimiant === 0) { + return (-b + Math.sqrt(discrimiant)) / 2 * a; + } + else { + var answers = {}; + answers["solution_one"] = (-b + Math.sqrt(discrimiant)) / (2 * a); + answers["solution_two"] = (-b - Math.sqrt(discrimiant)) / (2 * a); + return answers; + } +} + +// ES6 way + +let quadratic_formula = (a, b, c) => { + + let discrimiant = b**2 - (4 * a * c); + if (discrimiant < 0) { + return "This equation does not have a real solution"; + } + else if (discrimiant === 0) { + return (-b + Math.sqrt(discrimiant)) / 2 * a; + } + else { + let answers = {}; + answers["solution_one"] = (-b + Math.sqrt(discrimiant)) / (2 * a); + answers["solution_two"] = (-b - Math.sqrt(discrimiant)) / (2 * a); + return answers; + } +} \ No newline at end of file diff --git a/algebra/sum_of_n_terms_of_gp/javascript/sum_of_GP.js b/algebra/sum_of_n_terms_of_gp/javascript/sum_of_GP.js new file mode 100644 index 000000000..a3b9f6bff --- /dev/null +++ b/algebra/sum_of_n_terms_of_gp/javascript/sum_of_GP.js @@ -0,0 +1,9 @@ +// ES5 way + +function sum_of_gp(n, r, a) { + return (a*(1-Math.pow(r,n)))/(1-r); +} + +// ES6 way + +let sum_of_gp = (n, r, a) => (a*(1-Math.pow(r,n)))/(1-r); \ No newline at end of file diff --git a/geometry/arc_angle/javascript/arc_angle.js b/geometry/arc_angle/javascript/arc_angle.js new file mode 100644 index 000000000..9a644974f --- /dev/null +++ b/geometry/arc_angle/javascript/arc_angle.js @@ -0,0 +1,9 @@ +// ES5 way + +function arc_angle(arc_len, radius) { + return (360*arc_len)/(Math.PI*2*radius); +} + +// ES6 way + +let arc_angle = (arc_len, radius) => (360*arc_len)/(Math.PI*2*radius); \ No newline at end of file