// sphere.js #2
// This script calculates the volume of a sphere.
// Function called when the form is submitted.
// Function performs the calculation and returns false.
function calculate() {
'use strict';
// For storing the volume:
var volume;
// Task 1: Get a reference to the form element:
var radius = document.getElementById("radius");
// Add an "if" statement here to
// make sure there is a reference
if (radius) {
//Task #2: Add an "if" to make sure the value is positive:
if (radius > 0) {
// Task #3: Perform the calculation:
volume = (4/3)*(22/7)*(Pow(radius,3));
//HINT: the formula for the volume of a sphere is V=(4/3)*(pi)*(radius cubed)
// Format the volume:
volume = volume.toFixed(4);
// Task #4: Display the volume:
document.getElementById("volume").id ="volume";
//Hint: use the same method as in the radius variable assignment call above
} //End if
} end if radius
// Return false to prevent submission:
return false;
} // End of calculate() function.
// Function called when the window has been loaded.
// Function needs to add an event listener to the form.
function init() {
'use strict';
document.getElementById('theForm').onsubmit = calculate;
} // End of init() function.
window.onload = init;
我想制作一个脚本来计算球体的体积。它是一项家庭作业,这就是为什么所有这些评论都在那里。它基本上告诉我该怎么做。试图制作一个JavaScript程序来计算球体的体积
那么我遵循它的最好的我的知识,但我仍然得到一个错误。我得到的错误是第30行的“SyntaxError:missing; before statement”。这是告诉我把一个;之前“结束如果”。我猜这不是错误。我猜测公式就是错误。
条件由右括号“结束”。你应该完全摆脱“如果半径结束”。 – rnevius
您需要采用radius元素的'.value'。另外,“Pow”是什么?有没有理由不使用'Math.PI'? –
Javascript中没有'end if'语句。这应该是一个评论。 – jfriend00