Javascript 函数乘以其他函数的返回值,然后除以144

Javascript 函数乘以其他函数的返回值,然后除以144,javascript,html,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

我想知道我的语法是否正确。我想要一个函数乘以另外两个函数的返回值,然后除以结果。 我已经包括了下面的脚本。查看第三个函数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;
}
//设置关联数组
//这些键代表植物的大小
//这些值表示每个工厂所需的平方英寸
var square_inches=新阵列();
平方英寸[“种子”]=16;
平方英寸[“幼苗”]=16;
平方英寸[“少年”]=64;
平方英寸[“成人”]=144;
//设置关联数组
//这些键代表植物的数量
//该值表示数字的大小
//当用户从forms first select框中选择一个数字时,我们使用这个数组
var num_plant=新数组();
植物数量[“无”]=0;
num_plant[1]=1;
植物数量[2]=2;
植物数量[3]=3;
植物数量[4]=4;
植物数量[5]=5;
植物数量[6]=6;
植物数量[7]=7;
植物数量[8]=8;
植物数量[9]=9;
植物数量[10]=10;
植物数量[11]=11;
植物数量[12]=12;
植物数量[13]=13;
植物数量[14]=14;
植物数量[15]=15;
植物数量[16]=16;
植物数量[17]=17;
植物数量[18]=18;
植物数量[19]=19;
植物数量[20]=20;
num_plant[21]=21;
植物数量[22]=22;
植物数量[23]=23;
植物数量[24]=24;
植物数量[25]=25;
var type_grow=新数组();
类型_grow[select]=0;
_型生长[trad]=15;
类型_生长[水力]=21;
类型_grow[undec]=0;
//此函数用于根据
//下拉选择
函数getNumberOfPlants()
{
var userNumberChoice=0;
//获取对表单id=“sqfoot”的引用
var theForm=document.forms[“sqfoot”];
//获取对select id=“plantNumber”的引用
var selectedPlantNum=form.elements[“plantNumber”];
//将userNumberChoice设置为等于用户选择的值
//例如,num_plant[5.value]将等于5
userNumberChoice=num_plant[selectedPlantNum.value];
//最后我们返回userNumberChoice
返回userNumberChoice;
}
//此函数用于查找表示平方英寸的数字
//这里,我们需要从单选按钮选择中获取用户的选择
函数getSquareInches()
{  
var-thePlantSize=0;
//获取对表单id=“sqfoot”的引用
var theForm=document.forms[“sqfoot”];
//获取用户选择的工厂大小的参考名称=selectedPlant”:
var selectedPlant=form.elements[“plantType”];
//由于选择了4个单选按钮,因此此处显示Plant.length=4
//我们循环浏览每个单选按钮
对于(变量i=0;i
第三个函数的语法正确

虽然我会把它简化为:

function calculateSquareFeet(){
  return getNumberOfPlants() * getSquareInches() / 144; 
}
但这并不意味着你写的不正确。 不需要方形表。 从技术上讲,它浪费了一点ram和几个CPU周期。
但这并没有错。

您已经实现了所需的正确函数,但是就语法而言,您在构造
类型\u grow
数组时犯了错误

应该是:

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

对于JavaScript中关联数组的进一步阅读,这个站点有一个很好的解释:

那么什么是错误的?…它非常完美,你真的需要考虑使用一个循环来构建<代码> NUMIONGROUP>代码>。在我的550个字符中列出的字符太多。它甚至不会解析,因为类型_grow中的键被破坏了。代码目前是一堆意大利面,需要30分钟才能有人开始回答问题。@JohnGreen我认为不看代码,您应该关注问题标题Javascript函数,该函数将其他函数的返回值乘以,然后除以144@Pilot-代码是上下文,而且上下文被严重破坏了。@MatthewBlancarte深入到JSFIDLE或W3学校进行测试,您当然可以将这些值分配给这些键。我可能应该说您不应该将这些键分配给数组。您不再使用严格意义上的数组
Array
。您实际上是将其用作对象。当然,它可以工作,但是尝试获取“关联数组”的长度,您会发现它是0,即使您有四个项目。这是分配关联数组的正确javascript方法-如果您不使用
新数组()
[]初始化它,它将失败,因为
无法设置未定义的属性
首先。OP已经在他的代码中使用了一些,所以应该知道如何引用其中的项目(就像他对
平方英寸
数组所做的那样)。当然,如果你愿意,你也可以用
新对象()
分配它,但我认为人们使用
新数组()
可能更常见,因为,它的功能是作为一个关联数组(在本例中),因此增加了代码的清晰度,因为一眼就能看出它的乐趣所在