我有一个简单的计算器,可以用来计算几个形状的尺寸。应该对其进行设置,以便第一个函数具有一组 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.
}
最佳答案
你的语法不符合你的想法。例如,这一行是错误的:
if(firstQuestion === "Circle" || "Circle Area") { //If the answer is one of these strings, then it triggers the circle area function.
这不会检查 firstQuestion
是否是这些字符串中的任何一个。它检查两个条件之一是否为真或“真实”:(1) firstQuestion ===“Circle”
或 (2) “Circle Area”
。也就是说,它将“Circle Area”视为 bool 表达式,而不是使用 ===
进行比较的一部分。
“Circle Area”
始终为 true(换句话说,将其视为 bool 值,其计算结果为 true
)。因此,您的 if
条件将始终得到满足。同样的事情发生在其他每个 if
语句中。
对于每个条件,您都需要进行两次比较,如下所示:
if(firstQuestion === "Circle" || firstQuestion === "Circle Area") {
您还需要尽早添加一些 else
或 return
,因此您的 main 函数需要如下所示:
//Main Function
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!!!"); //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.
calcCircleArea();
}
else if(firstQuestion === "Sphere" || firstQuestion === "Sphere Volume") { //This one triggers the sphere volume function.
calcSphereVolume();
}
else if(firstQuestion === "Prism" || firstQuestion === "Rectangular Prism" || firstQuestion === "Prism Volume" || firstQuestion === "Rectangular Prism Volume") { //This one is for volume of a 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!");
}
}
更好的解决方案是使用 switch
/case
/default
block ,但这并不是绝对必要的。
关于javascript - 隐藏变量直到需要为止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30498476/