Ich versuche JavaScript zu schreiben, das einen Benutzer nach Daten fragt, eine Funktion ausführt, um eine Berechnung für diese Daten durchzuführen, und dann eine Warnung zum Anzeigen dieser Daten zu erstellen.Wie übergebe ich Variablen und Funktionen an eine Alarmfunktion?
Derzeit dieser Code hat zwei Funktionen: calculateArea und displayArea:
calculateArea
function calculateArea(myRadius) {
//sets the variable 'myArea' to radius^2 * pi
var myArea = (myRadius * myRadius * math.pi);
//returns the variable myArea to the global function.
return myArea;
}
displayArea
function displayArea() {
//the code for the alert
alert("A circle with a " + myRadius + " centimeter radius has an area of " + myArea + " centimeters.");
}
Erwartete Ausgabe: Die Webseite sollte th auffordern e Benutzer für ihren Radius und dann eine Warnung, die die Flächenberechnung anzeigt.
Tatsächliche Ausgabe: Die Webseite fordert den Benutzer auf den Radius, und das tut nichts anderes.
Hier ist der Körper des HTML-Dokuments:
<body>
<script>
// Performs the calculateArea function on the myRadius variable.
function calculateArea(myRadius) {
//sets the variable 'myArea' to radius^2 * pi
var myArea = (myRadius * myRadius * math.pi);
//returns the variable myArea to the global function.
return myArea;
}
//Performs an alert that shows the area and radius of the circle.
function displayArea() {
//the code for the alert
alert("A circle with a " + myRadius + " centimeter radius has an area of " + myArea + " centimeters.");
}
//prompts the user for the circle radius.
var myRadius = parseFloat(prompt("Enter the radius of your circle in centimeters: ",));
//runs the calculateArea function on the myRadius variable entered by the user.
calculateArea(myRadius);
//runs the displayArea function, showing the alert.
displayArea();
</script>
</body>
Sie einen Syntaxfehler haben, entfernen Sie das Komma aus var myRadius ... Linie und btw Warnungen verwendet, ist so 90er Jahre. –