2017-09-17 4 views
-1

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> 
+0

Sie einen Syntaxfehler haben, entfernen Sie das Komma aus var myRadius ... Linie und btw Warnungen verwendet, ist so 90er Jahre. –

Antwort

0

Sie müssen:

  • schreiben Math.PI mit der genauen uppper/Kleinschreibung.
  • Holen Sie sich das Ergebnis der Berechnung in einer Variablen
  • Übergeben Sie Variablen an die zweite Funktion und vermeiden Sie, sich auf globale Variablen zu verlassen.

Hier ist, wie es funktionieren kann:

function calculateArea(myRadius) { 
 
    // Math needs a capital, and PI is all caps 
 
    var myArea = myRadius * myRadius * Math.PI; 
 
    return myArea; 
 
} 
 

 
// Add arguments to this function: 
 
function displayArea(myRadius, myArea) { 
 
    alert("A circle with a " + myRadius + " centimeter radius has an area of " + myArea + " centimeters."); 
 
} 
 

 
var myRadius = parseFloat(prompt("Enter the radius of your circle in centimeters: ",)); 
 

 
// store the result 
 
var myArea = calculateArea(myRadius); 
 
displayArea(myRadius, myArea); // pass the input and result that this function needs

0

hier Ihren Code behoben.

Hier ist, was geändert:

  1. math.pi als Math.PI

  2. aktiviert werden müssen, genauso wie Sie Ihre Aufforderung an den myRadius Variable sind zu speichern, müssen Sie auch das Ergebnis von calculateArea() auf ein speichern Variable. Ich setze es auf myArea, nachdem es fertig ist.

// 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. 
 
var myArea = calculateArea(myRadius); 
 

 
//runs the displayArea function, showing the alert. 
 
displayArea();

0

Sie kehren myArea aber nicht irgendwo zu speichern und es ist Math.PI nicht math.pi. Da myRadius global definiert ist, können Sie es auch in anderen Funktionen verwenden.

var myArea = calculateArea(myRadius); // gets the return myArea value and defined globally 

<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. 
 
var myArea = calculateArea(myRadius); 
 

 
//runs the displayArea function, showing the alert. 
 
displayArea(); 
 

 
</script>

Verwandte Themen