如何让我的while循环工作?

时间:2010-02-21 16:43:26

标签: javascript arrays loops while-loop

我有一个程序,其中我编写了三个函数,差异(计算数组之间的数字差异)总和(总计数组)和计算差异,它使用差异函数来确定下一个数组应该是什么)。

所有这些功能都在运行,但是我在主程序中使用while循环时遇到了问题,我无法看到我出错的地方!

如果有人能指出我正确的方向,那么我会很感激。该程序应该写出初始数组。然后它计算新数组并将其写出(到目前为止)。然后我需要循环它,以便总和不是0,程序运行并计算重复该过程以达到0所需的次数。我认为我必须设置numberArray的值返回从计算差异的新数字,然后清除计算差异阵列。我已经在这方面工作了好几天,而且我也没有找到我需要做的事情了。我不想让人们给我答案,因为这是我的课程作业,但我想得到一些关于我出错的指导。

function difference(firstNumber, secondNumber)
{
    if (firstNumber > secondNumber)
    {
        return (firstNumber - secondNumber);
    }    
    else
    {   
        return (secondNumber - firstNumber);
    }    

}

function sum(numberArray)
{
    numberTotal = 0
    for (var total = 0; total < numberArray.length; total = total + 1)
    {
        numberTotal = numberTotal + numberArray[total]
    }
        {
            return numberTotal
        }

}
function calculateDifferences()
{
    var createArray = new Array(numberArray.length); 
    for (var c = 0; c < numberArray.length - 1 ; c = c + 1) 
    {
    createArray[c] = difference(numberArray[c],numberArray[c+1]); 
    }
        {
            createArray[numberArray.length - 1] = difference(numberArray[0],numberArray[numberArray.length - 1]);
        }  
            {        
                return createArray; 
            }
}

var numberArray = [16,14,4,5];//initial numbers to start with
document.write(numberArray +'<BR>');//writes out initial numbers
sum(numberArray);// checks to see if sum total = 0
var count = 0;// delcares the counter to 0

while(sum(numberArray) > 0)// runs the programme while sum is not 0
{
    count = count + 1;// counts how many times looped

    calculateDifferences(numberArray);//calculates the new numbers from numberArray

    document.write (calculateDifferences() + '<BR>');//writes out new numbers

    calculateDifferences = numberArray;// sets the numberArray to new figures

    calculateDifferences() = 0;//clears array for next calculate

    sum (numberArray);//checks the condition again

}

document.write ( 'interations taken = ' + count + '<BR>');//if sum 0 then programme  finishes by writing out how many times it took to get to 0

4 个答案:

答案 0 :(得分:0)

在循环的第三行中,再次调用calculateDifferences()而不传入参数。也许在第二行中,将calculateDifferences(numberArray)返回的值保存到变量中,然后写出变量。

编辑:同样,您无法通过将数组设置为0来清除数组

答案 1 :(得分:0)

您的问题是这些:

calculateDifferences = numberArray;// sets the numberArray to new figures
calculateDifferences() = 0;//clears array for next calculate

它们没有意义。
第一次调用试图将函数设置为等于你的数组?
第二个调用是将函数设置为0?

您可以使用您的函数执行计算和返回值,但不应将函数设置为等于某些值。

您要做的是调用一次CalculationDifferences并将其输出存储在变量中。之后,您可以重新分配numberArray变量以指向新数组。您也不必担心先清理它。

此外,你做错了但不会导致你的代码破坏:

  1. 您的calculateDifferences不需要任何参数。所以你不需要传递numberArray。
  2. 你不需要在循环结束时调用sum(numberArray),你的循环条件每次都会检查。特别是你不需要它,因为你没有使用它的任何返回值。

答案 2 :(得分:0)

四件事。首先,你的一些功能中的额外括号不做任何事情。与C不同,javascript没有大括号范围,只有函数范围。在我看来,删除它们会使代码更具可读性。

其次,差异函数应该只是Math.abs(firstNumber-secondNumber)。这不仅更短,而且更具可读性。

第三,你的目标是:

while( checkSomeCondition(myArray) ) {
    myArray = doSomethingWith(myArray);
}

不要担心释放数组的旧数据。当没有任何东西引用它时,它将自动被垃圾收集。

第四,我个人会修改calculateDifferences函数以将数组作为参数,而不是将数组作为全局变量传递。

答案 3 :(得分:0)

createArray [numberArray.length - 1] = difference(numberArray [0],numberArray [numberArray.length - 1]);我认为这是问题,

尝试createArray [3] =差异(numberArray [0],numberArray [3]);