我的区域计算器运行错误,并没有给我一个错误。我是初学者,所以我不知道发生了什么

时间:2017-07-25 19:49:49

标签: javascript

alert("Welcome to the area calculator!");
prompt("Would you like to calculate the area of a 2 dimensional or 3 dimensional object? Type in either 2D or 3D") === objectDimension;
// User chooses dimension
if (objectDimension === "2d") {
  // user chooses shape
  prompt("What shape would you like to find the area of? You can choose from a rectangle, circle, or triangle.") === chosenShape;
} else if (objectDimension === "3d") {
  // user chooses 3D object
  prompt("What 3D object would you like to find the area of. You can choose anything as long as it's a cube.") === chosenShape;
}

function calculateAreaRectangle(Length, Width) {
  alert("The area of the rectangle is " + Length * Width + " units.");
}

function calculateAreaCube(Length, Width, Height) {
  alert("The area of the cube is " + Length * Width * Height + " units.");
}

function calculateAreaTriangle(Base, Height) {
  alert("The area of the triangle is " + Base * Height * .5 + " units.");
}

function calculateAreaCircle(Radius) {
  alert("The area of the circle is approximately " + 3.14 * Radius * Radius + "units, if 3.14 is used for pi.");
}

// 3D shape function calling
if (chosenshape === "cube") {
  calculateAreaCube(
    prompt("What is the length of this cube?"),
    prompt("What is the width of this cube?"),
    prompt("What is the height of this cube?")
  )
}
// 2D shape function calling
else if (chosenshape === "rectangle") {
  calculateAreaRectangle(
    prompt("What is the length of this rectangle?"),
    prompt("What is the width of this rectangle?")

  )
} else if (chosenshape === "circle") {
  calculateAreaCircle(
    prompt("What is the radius of this circle?")

  )
} else if (chosenshape === "triangle") {
  calculateAreaTriangle(
    prompt("What is the base of this triangle?"),
    prompt("What is the height of this triangle?")

  )
}

我不明白为什么我的区域计算器不起作用。当我使用它直接到立方体,并没有给我一个错误。我究竟做错了什么?我是一个完整的编程初学者,所以任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

看起来您可能会对如何为变量赋值给您感到困惑。

prompt("Would you like to calculate the area of a 2 dimensional or 3 dimensional object? Type in either 2D or 3D") === objectDimension;

但是这会将prompt(...)objectDimension的结果进行比较,而结果是控制台告诉您的结果。你想要做的是首先声明你的变量然后分配它,例如:

var objectDimension = prompt("Would you like to calculate the area of a 2 dimensional or 3 dimensional object? Type in either 2D or 3D");

分配和比较的更完整示例:

var result;

result = prompt("Enter 1 or 2");

if(result === "1"){
    alert("You entered the number one!")
} else if(result == "2") {
    alert("You entered the number 2!")
} else {
    alert("Y u no listen")
}

=====用于比较,=用于分配。您可能需要阅读thisthis以获取更多帮助

答案 1 :(得分:0)

你有几个问题。

1)将提示的值分配给这样的变量。   var value = prompt(“caption”);

2)变量区分大小写。你有chosenshape和selectedShape。选择一个并坚持下去。

alert("Welcome to the area calculator!");

// set objectDimension to the value of the prompt call
var objectDimension = prompt("Would you like to calculate the area of a 2 dimensional or 3 dimensional object? Type in either 2D or 3D");

// User chooses dimension



// Recommended to use .trim() and .toLower() on the prompt value to remove     spaces and change it to 2d (incase the entered 2D)


if (objectDimension.trim().toLowerCase() === "2d") {
    // user chooses shape
    var chosenShape = prompt("What shape would you like to find the area of? You can choose from a rectangle, circle, or triangle.");
} else if (objectDimension.trim().toLowerCase() === "3d") {
    // user chooses 3D object
    var chosenShape = prompt("What 3D object would you like to find the area of. You can choose anything as long as it's a cube.");
}

function calculateAreaRectangle(Length, Width) {
    alert("The area of the rectangle is " + Length * Width + " units.");
}

function calculateAreaCube(Length, Width, Height) {
    alert("The area of the cube is " + Length * Width * Height + " units.");
}

function calculateAreaTriangle(Base, Height) {
    alert("The area of the triangle is " + Base * Height * .5 + " units.");
}

function calculateAreaCircle(Radius) {
    alert("The area of the circle is approximately " + 3.14 * Radius * Radius + "units, if 3.14 is used for pi.");
}

// 3D shape function calling
// variables are case sensitive.  changed to chosenShape from chosenshape
if (chosenShape === "cube") {
    calculateAreaCube(
        prompt("What is the length of this cube?"),
        prompt("What is the width of this cube?"),
        prompt("What is the height of this cube?")
    )
}
// 2D shape function calling
else if (chosenShape === "rectangle") {
    calculateAreaRectangle(
        prompt("What is the length of this rectangle?"),
        prompt("What is the width of this rectangle?")

    )
} else if (chosenShape === "circle") {
    calculateAreaCircle(
        prompt("What is the radius of this circle?")

    )
} else if (chosenShape === "triangle") {
    calculateAreaTriangle(
        prompt("What is the base of this triangle?"),
        prompt("What is the height of this triangle?")

    )
}