隐藏变量直到需要?
Hide variables until needed?
我有一个简单的计算器可以计算几个形状的尺寸。它应该被设置为使第一个函数有一组 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")
总是评估为 true
因为 "Circle Area"
是一个真实的陈述。
尝试将其更改为 if(firstQuestion === "Circle" || firstQuestion === "Circle Area")
.
之类的内容
你的语法不符合你的想法。例如,这一行是错误的:
if(firstQuestion === "Circle" || "Circle Area") { //If the answer is one of these strings, then it triggers the circle area function.
这不会检查 firstQuestion
是否是这些字符串中的任何一个。它检查两个条件之一是否为真或 "truthy":(1) firstQuestion === "Circle"
或 (2) "Circle Area"
。也就是说,它将 "Circle Area"
视为布尔表达式,而不是使用 ===
.
进行比较的一部分
"Circle Area"
始终为真(换句话说,被视为布尔值,它的计算结果为 true
)。因此,您的 if
条件将始终得到满足。同样的事情发生在其他每个 if
语句中。
对于您的每个条件,您需要进行两次比较,如下所示:
if(firstQuestion === "Circle" || firstQuestion === "Circle Area") {
您还需要提前添加几个 else
或 return
,因此您的主要功能需要如下所示:
//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
块,但这并不是绝对必要的。
Matthew,你应该使用 Switch 语句来进行这种选择。更重要的是你的代码中有很多错误。例如,您使用了 var p,但您在函数中使用了未定义的 pi 变量。我已经为你修好了。如果这个答案有帮助,请通过投票让我知道。
我已经为你制作了工作示例 JSFIDDLE --> http://jsfiddle.net/zpo4b3mo/
//Lee_Matthew_Functions_Assignment_5/26/2015
//Main Function
var pi = 3.14; //pi rounded down to the hudredths place.
function theConjurer() {
var 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!!!");
switch(firstQuestion) {
case "Circle" || "Circle Area":
console.log('Selected Circle');
calcCircleArea();
break;
case "Sphere" || "Sphere Volume":
console.log('Selected Sphere');
calcSphereVolume();
break;
case "Prism" || "Rectangular Prism" || "Prism Volume" || "Rectangular Prism Volume":
console.log('Selected Prism');
calcPrismVolume();
break;
default: //Anything else just brings up another prompt for the same answers.
console.log('None match');
//firstQuestion = prompt("Sorry, that's not one of the options I have right now. Try again!");
break;
}
}
theConjurer(); //To automatically bring up the starting function.
//Function for Dimensions of a Circle
function calcCircleArea() {
var radius = prompt('What is the radius?');
var circleArea = pi * Math.pow(parseInt(radius), 2);
console.log("The circle you've entered is " + circleArea + " square units!");
}
//Function for Volume of a Sphere
function calcSphereVolume() {
var f = (4/3);
var r = 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.
var sphereVolume = f * pi * Math.pow(r, 3);
console.log("Your sphere is " + sphereVolume + " cubed units! Congradulations!");
}
//Function for Dimensions of a Rectangular Prism
function calcPrismVolume() {
var l = prompt("What's the length of this prism?"); //User input for the shape's length.
var w = prompt("What's the width?"); //User input for the shape's width.
var h = prompt("What is the height?"); //User input for the shape's height.//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
语句,这些语句根据用户输入触发一个且仅一个其他函数。但相反,无论我的回答是什么,它都会触发所有这些。这是因为所有变量提示都是公开的,并且只是 运行 全部通过它们。我如何让它只运行与函数 运行 对应的那些?它也使我所有的 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")
总是评估为 true
因为 "Circle Area"
是一个真实的陈述。
尝试将其更改为 if(firstQuestion === "Circle" || firstQuestion === "Circle Area")
.
你的语法不符合你的想法。例如,这一行是错误的:
if(firstQuestion === "Circle" || "Circle Area") { //If the answer is one of these strings, then it triggers the circle area function.
这不会检查 firstQuestion
是否是这些字符串中的任何一个。它检查两个条件之一是否为真或 "truthy":(1) firstQuestion === "Circle"
或 (2) "Circle Area"
。也就是说,它将 "Circle Area"
视为布尔表达式,而不是使用 ===
.
"Circle Area"
始终为真(换句话说,被视为布尔值,它的计算结果为 true
)。因此,您的 if
条件将始终得到满足。同样的事情发生在其他每个 if
语句中。
对于您的每个条件,您需要进行两次比较,如下所示:
if(firstQuestion === "Circle" || firstQuestion === "Circle Area") {
您还需要提前添加几个 else
或 return
,因此您的主要功能需要如下所示:
//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
块,但这并不是绝对必要的。
Matthew,你应该使用 Switch 语句来进行这种选择。更重要的是你的代码中有很多错误。例如,您使用了 var p,但您在函数中使用了未定义的 pi 变量。我已经为你修好了。如果这个答案有帮助,请通过投票让我知道。
我已经为你制作了工作示例 JSFIDDLE --> http://jsfiddle.net/zpo4b3mo/
//Lee_Matthew_Functions_Assignment_5/26/2015
//Main Function
var pi = 3.14; //pi rounded down to the hudredths place.
function theConjurer() {
var 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!!!");
switch(firstQuestion) {
case "Circle" || "Circle Area":
console.log('Selected Circle');
calcCircleArea();
break;
case "Sphere" || "Sphere Volume":
console.log('Selected Sphere');
calcSphereVolume();
break;
case "Prism" || "Rectangular Prism" || "Prism Volume" || "Rectangular Prism Volume":
console.log('Selected Prism');
calcPrismVolume();
break;
default: //Anything else just brings up another prompt for the same answers.
console.log('None match');
//firstQuestion = prompt("Sorry, that's not one of the options I have right now. Try again!");
break;
}
}
theConjurer(); //To automatically bring up the starting function.
//Function for Dimensions of a Circle
function calcCircleArea() {
var radius = prompt('What is the radius?');
var circleArea = pi * Math.pow(parseInt(radius), 2);
console.log("The circle you've entered is " + circleArea + " square units!");
}
//Function for Volume of a Sphere
function calcSphereVolume() {
var f = (4/3);
var r = 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.
var sphereVolume = f * pi * Math.pow(r, 3);
console.log("Your sphere is " + sphereVolume + " cubed units! Congradulations!");
}
//Function for Dimensions of a Rectangular Prism
function calcPrismVolume() {
var l = prompt("What's the length of this prism?"); //User input for the shape's length.
var w = prompt("What's the width?"); //User input for the shape's width.
var h = prompt("What is the height?"); //User input for the shape's height.//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.
}