将其他函数的返回值相乘的函数则除以144

时间:2014-02-21 05:42:34

标签: javascript html

我想知道我是否有正确的语法。我想要一个函数来乘以其他两个函数的返回值然后除以结果。 我已经包含了下面的脚本。看看第三个函数calculateSquareFeet。我指的是var theSquareFeet。

//Set up an associative array
 //The keys represent the size of the plants
 //The values represent the square inches required per plant 
 var square_inches = new Array();
 square_inches ["seeds"]=16;
 square_inches ["seedlings"]=16;
 square_inches ["juveniles"]=64;
 square_inches ["adult"]=144;


 //Set up an associative array 
 //The keys represent the number of plants
 //The value represents the the number
 //We use this this array when the user selects a number from the forms first select box
 var num_plant= new Array();
    num_plant ["none"] = 0;
    num_plant [1] = 1;
    num_plant [2] = 2;
    num_plant [3] = 3;
    num_plant [4] = 4;
    num_plant [5] = 5;
    num_plant [6] = 6;
    num_plant [7] = 7;
    num_plant [8] = 8;
    num_plant [9] = 9;
    num_plant [10] = 10;
    num_plant [11] = 11;
    num_plant [12] = 12;
    num_plant [13] = 13;
    num_plant [14] = 14;
    num_plant [15] = 15;
    num_plant [16] = 16;
    num_plant [17] = 17;
    num_plant [18] = 18;
    num_plant [19] = 19;
    num_plant [20] = 20;
    num_plant [21] = 21;
    num_plant [22] = 22;
    num_plant [23] = 23;
    num_plant [24] = 24;
    num_plant [25] = 25;

var type_grow = new Array();
    type_grow [select] =0;
    type_grow [trad] =15;
    type_grow [hydro] =21;
    type_grow [undec] =0;



//This function finds the number of plants based on the 
//drop down selection
function getNumberOfPlants()
{
    var userNumberChoice=0;
    //Get a reference to the form id="sqfoot"
    var theForm = document.forms["sqfoot"];
    //Get a reference to the select id="plantNumber"
     var selectedPlantNum = theForm.elements["plantNumber"];

    //set userNumberChoice equal to value user chose
    //For example num_plant[5.value] would be equal to 5
    userNumberChoice = num_plant[selectedPlantNum.value];

    //finally we return userNumberChoice
    return userNumberChoice;
}




// This function finds a number that represents square inches
// Here, we need to take user's the selection from radio button selection
function getSquareInches()
{  
    var thePlantSize=0;
    //Get a reference to the form id="sqfoot"
    var theForm = document.forms["sqfoot"];
    //Get a reference to the plant size the user Chooses name=selectedPlant":
    var selectedPlant = theForm.elements["plantType"];
    //Here since there are 4 radio buttons selectedPlant.length = 4
    //We loop through each radio buttons
    for(var i = 0; i < selectedPlant.length; i++)
    {
        //if the radio button is checked
        if(selectedPlant[i].checked)
        {
            //we set thePlantSize to the value of the selected radio button
            //i.e. if the user choose Juvenile we set it to 32
            //by using the square_inches array
            //We get the selected Items value
            //For example square_inches["Juvenile".value]"
            thePlantSize = square_inches[selectedPlant[i].value];
            //If we get a match then we break out of this loop
            //No reason to continue if we get a match
            break;
        }
    }
    //We return thePlantSize
    return thePlantSize;
}

function calculateSquareFeet()
{
    /* Here we get the square feet by multiplying getNumberOfPlants() x getSquareInches() then divide by 144, the square inches in a square foot  */
    var theSquareFeet = (getNumberOfPlants() * getSquareInches()) / 144; 

        return theSquareFeet;
}

2 个答案:

答案 0 :(得分:0)

第三个函数的语法是正确的。

虽然我会把它减少到:

function calculateSquareFeet(){
  return getNumberOfPlants() * getSquareInches() / 144; 
}

但这并不能使你写的不正确。 不需要theSquareFeet。 从技术上讲,它浪费了一些内存和一些CPU周期。 但这没错。

答案 1 :(得分:0)

您已经创建了所需的正确函数,但就语法错误构建type_grow数组

而言

应该是:

var type_grow = new Array();
    type_grow ["select"] =0;
    type_grow ["trad"] =15;
    type_grow ["hydro"] =21;
    type_grow ["undec"] =0;

为了进一步阅读javascript中的关联数组,这个网站有一个很好的解释:http://www.i-programmer.info/programming/javascript/1441-javascript-data-structures-the-associative-array.html