2017-01-07 60 views
-1

我正在嘗試創建一個JavaScript程序,它將提示用戶輸入一個數字範圍「3-6」,然後輸出每個數字的面積,半徑和周長在範圍中。JavaScript的多個值的圓周,直徑和半徑

下面是我的代碼到目前爲止,我知道它目前不工作,但幾個指針會很棒!

function circum() { 
var radius = prompt("Enter the a range of numbers for the circle",""); 
var radiusanswer = 2 * Math.PI * radius; 
var area = radius * radius * Math.PI; 
var cube = radius * radius * radius 
alert("The circumference is "+radiusanswer) 
alert("The area is "+area) 
alert("The cube is "+cube) 

回答

1

你就近了。只是需要循環正確有效範圍內的所有號碼,如下面的感謝

function circum() { 
 
    var values = prompt("Enter the a range of numbers for the circle", ""); 
 
    var range = values.split("-"); 
 
    //range[0] = start value 
 
    //range[1] = end value 
 
    for (var i = range[0]; i <= range[1]; i++) { 
 
    var radius = i; 
 

 
    var radiusanswer = 2 * Math.PI * radius; 
 
    var area = radius * radius * Math.PI; 
 
    var cube = radius * radius * radius 
 
    console.log("The circumference is " + radiusanswer) 
 
    console.log("The area is " + area) 
 
    console.log("The cube is " + cube) 
 
    } 
 

 
} 
 
circum();

+0

嘿非常的幫助。一旦輸入兩個數字,我就會看到一個空白屏幕,在輸入值後,如何在頁面上輸出控制檯日誌? – user1521810

+0

將值附加到標記 –