// 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