【发布时间】:2015-08-10 11:37:15
【问题描述】:
我有一个简单的计算器来计算几个形状的尺寸。它应该被设置为使第一个函数具有一组if 语句,这些语句根据用户输入触发一个且仅一个其他函数。但相反,无论我的答案是什么,它都会触发所有这些。这是因为所有变量提示都是公开的,它只是通过它们全部运行。我如何使它只运行与正在运行的函数相对应的函数?它也成功了,所以我所有的 console.log 结果都出现了 NaN。我该如何解决这个问题?
//主要功能
function theConjurer() {
firstQuestion = prompt("Hi! This is my shape dimension calculator! I have three different shapes in this version but I can always add more! For now I have these three:\nCircle Area\nSphere Volume\nRectangular Prism Volume\n\nEnjoy!!!");
return firstQuestion.toLowerCase(); //The answer to this prompt is supposed to spring one of the functions below.
if(firstQuestion === "Circle" || firstQuestion === "Circle Area") { //If the answer is one of these strings, then it triggers the circle area function.
console.log("You've slected Circle!")
calcCircleArea();
}
else if(firstQuestion === "Sphere" || firstQuestion === "Sphere Volume") { //This one triggers the sphere volume function.
console.log("You've slected Sphere!")
calcSphereVolume();
}
else if(firstQuestion === "Prism" || firstQuestion === "Rectangular Prism" || firstQuestion === "Prism Volume" || firstQuestion === "Rectangular Prism Volume") { //This one is for volume of a rectangular prism.
console.log("You've slected Rectangular Prism!")
calcPrismVolume();
}
else { //Anything else just brings up another prompt for the same answers.
firstQuestion = prompt("Sorry, that's not one of the options I have right now. Try again!");
}
}
theConjurer(); //To automatically bring up the starting function.
//Function for Dimensions of a Circle
var pi = 3.14; //pi rounded down to the hudredths place.
var circleRadius = prompt("What is the radius of your circle?")
calcCircleArea(pi, circleRadius); //Variable declarations.
function calcCircleArea(p, r) { //
var circleArea = p * Math.pow(r, 2);
console.log("The circle you've entered is " + circleArea + " square units!");
}
//Function for Volume of a Sphere
var fourThirds = (4/3);
var radius = prompt("Do you know the diameter of your sphere? Just half that is your radius! What's the radius?"); //User input for the shape's radius.
calcSphereVolume(fourThirds, pi, radius);
function calcSphereVolume(f, p, r) {
var sphereVolume = f * p * Math.pow(r, 3);
console.log("Your sphere is " + sphereVolume + " cubed units! Congradulations!");
}
//Function for Dimensions of a Rectangular Prism
var length = prompt("What's the length of this prism?"); //User input for the shape's length.
var width = prompt("What's the width?"); //User input for the shape's width.
var height = prompt("What is the height?"); //User input for the shape's height.
calcPrismVolume(length, width, height); //Storage for the above three variables.
function calcPrismVolume(l, w, h) { //Function and its parameters.
var prismVolume = l * w * h; //New variable made from multiplying those parameters.
console.log("Your prism is " + prismVolume + " cubed units!"); //Output of shape's area.
}
【问题讨论】:
-
||OR 运算符用于条件列表而不是值列表...所以它是variable operator value1 || variable operator value2而不是variable operator value1 || value2。试试if(firstQuestion === "Prism" || firstQuestion === "Rectangular Prism")等 -
需要注意的是
["Sphere", "Sphere volume"].indexOf(firstQuestion) !== -1可以做,但是不能用OR加多个值,它只是分隔语句 -
我可能不得不尝试 [ ] 选项。我使用 OR 运算符进行了您建议的更改,现在我输入的任何答案都会触发 else 语句。
-
只要使用你的调试器,不要再问这样的基本问题了。对不起,这是我的看法。如果您查找“javascrpt prompt”、“javascript NaN”、“javascript type cast”等,您会在 Google 上找到十几个示例。
标签: javascript if-statement parameters var