]> git.za3k.com Git - za3k.git/commitdiff
add premade functions to eval.html
authorZachary Vance <za3k@za3k.com>
Sat, 9 Jul 2022 04:10:50 +0000 (00:10 -0400)
committerZachary Vance <za3k@za3k.com>
Sat, 9 Jul 2022 04:10:50 +0000 (00:10 -0400)
eval.html

index b4905632aa2afb6d5fdc0e4a0f9cde78a4736cde..422d28abecc48331e658cb5678777a4cf5c5c17c 100644 (file)
--- 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);
+}
+
 </script>
 
 
@@ -134,12 +190,21 @@ label {
 <title>eval.js</title>
 </head>
 <body>
+<div class="premade">
+    <label>Premade</label>
+    <select id="premade">
+        <option value="area_of_circle" selected>Area of a Circle</option>
+        <option value="area_of_triangle1">Area of a Triangle (from sides)</option>
+        <option value="area_of_triangle2">Area of a Triangle (from height)</option>
+        <option value="volume_of_cylinder">Volume of a Cylinder</option>
+        <option value="volume_of_sphere">Volume of a Sphere</option>
+        <option value="volume_of_pyramid">Volume of a Pyramid</option>
+        <option value="surface_area_of_sphere">Surface Area of a Sphere</option>
+        <option value="surface_area_of_cylinder">Surface Area of a Cylinder</option>
+        <option value="weight_of_cylinder">Weight of a Cylinder</option>
+        <option value="weight_of_sphere">Weight of a Sphere</option>
+    </select>
 <div class="definition"><label>Function</label><pre id="formula" contenteditable="true">
-// Javascript
-// Edit me and change my arguments!
-function(r, h) {
-    return Math.PI * Math.pow(r, 2) * h;
-}
 </pre></div>
 <div class="error"><label>Errors</label><span id="error">s</span></div>
 <div class="inputs" id="inputs"></div>