$("#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());
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>
<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>