From: Zachary Vance Date: Sat, 9 Jul 2022 04:10:50 +0000 (-0400) Subject: add premade functions to eval.html X-Git-Url: https://git.za3k.com/?a=commitdiff_plain;h=4c4940b57522375ca4d93aa19b2515f3aac910e7;p=za3k.git add premade functions to eval.html --- diff --git a/eval.html b/eval.html index b490563..422d28a 100644 --- a/eval.html +++ b/eval.html @@ -71,6 +71,13 @@ function recalc() { $("#result").text(result); } +function load_function(func_name) { + const f = window[func_name]; + const source = f.toString(); + HEADER = "// Javascript\n// Edit me and change my arguments!\n" + $("#formula").text(HEADER + source); +} + $(function() { $("#formula").on("input", function() { redefine($("#formula").text().trim()); @@ -80,11 +87,60 @@ $(function() { load_inputs(); recalc(); }); + $("#premade").on("input", function() { + load_function($("#premade").val()); + redefine($("#formula").text().trim()); + load_inputs(); + recalc(); + }); + load_function($("#premade").val()); redefine($("#formula").text().trim()); load_inputs(); recalc(); }); + +area_of_circle = function(r) { + return Math.PI * Math.pow(r, 2); +} +area_of_triangle1 = function(s1, s2, s3) { + const p = (s1 + s2 + s3) / 2; + return Math.sqrt(p*(p-s1)*(p-s2)*(p-s3)); +} +area_of_triangle2 = function(base, height) { + return 0.5 * base * height; +} +volume_of_sphere = function(r) { + return 4 / 3 * Math.PI * Math.pow(r, 3); +} +volume_of_cylinder = function(r, h) { + return Math.PI * Math.pow(r, 2) * h; +} +volume_of_pyramid = function(base_area, height) { + return 1 / 3 * base_area * height; +} +weight_of_cuboid = function(l, w, h, d) { + // d = 0.036 pounds per cubic inch is water + // d = 1000 kg per cubic meter is water, or 1g per cubic cm + return l * w * h * d; +} +weight_of_sphere = function(r, d) { + // d = 0.036 pounds per cubic inch is water + // d = 1000 kg per cubic meter is water, or 1g per cubic cm + return 4 / 3 * Math.PI * Math.pow(r, 3) * d; +} +weight_of_cylinder = function(r, h, d) { + // d = 0.036 pounds per cubic inch is water + // d = 1000 kg per cubic meter is water, or 1g per cubic cm + return Math.PI * Math.pow(r, 2) * h * d; +} +surface_area_of_sphere = function(r) { + return 4 * Math.PI * Math.pow(r, 2); +} +surface_area_of_cylinder = function(r, h) { + return 2 * Math.PI * r * (r + h); +} + @@ -134,12 +190,21 @@ label { eval.js +
+ +
-// Javascript
-// Edit me and change my arguments!
-function(r, h) {
-    return Math.PI * Math.pow(r, 2) * h;
-}
 
s