我正在尝试创建一个JavaScript程序,它会提示用户输入一系列数字'3-6',然后输出范围内每个数字的面积,半径和周长。
以下是我的代码到目前为止,我知道它目前无法正常工作,但有几个指针会很棒!
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)
答案 0 :(得分: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();
&#13;