隐藏变量直到需要?

时间:2015-05-28 06:09:14

标签: javascript if-statement parameters var

我有一个简单的计算器,用于查找几个形状的尺寸。它应该被设置为第一个函数有一组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.
        }

3 个答案:

答案 0 :(得分:1)

您的语法不符合您的想法。例如,这一行是错误的:

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"视为布尔表达式,而不是使用===进行比较的一部分。

"Circle Area"总是真实的(换句话说,被视为布尔值,它的计算结果为true)。因此,您的if条件将始终得到满足。每个其他if语句都会发生同样的事情。

对于您的每个条件,您需要进行两次比较,如下所示:

if(firstQuestion === "Circle" || firstQuestion === "Circle Area") { 

您还需要提前添加一些elsereturn,因此您的主要功能需要如下所示:

//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块,但这不是绝对必要的。

答案 1 :(得分:0)

if(firstQuestion === "Circle" || "Circle Area")始终评估为true,因为"Circle Area"是一个真实的陈述。 尝试将其更改为if(firstQuestion === "Circle" || firstQuestion === "Circle Area")

答案 2 :(得分:0)

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.
}