2015-04-01 108 views
0

扩展代码我一直在为补充跟踪器工作,但是我的当前函数没有返回大于平均“均值”的数字的精确计数,也没有返回低于均值平均值的整数计数。我也在代码中注释了两个问题,因为我不太明白为什么将数组设置为索引[0]。我从评论中学到了很多东西,并在这里寻找答案。非常感谢这个网站的存在!希望对这个问题有更多的希望。为什么我的函数没有返回精确的计数?

function suppArray() { 
var nums = new Array(); //create array 
var sum = 0; //variable to hold sum of integers in array 
var avg = 0; //variable to hold the average 
var i; 
var count = 0; 
var count2 = 0; 
var contents = ''; //variable to hold contents for output 

    var dataPrompt = prompt("How many numbers do you want to enter?", ""); 
    dataPrompt = parseInt(dataPrompt); 

    for(i = 0; i <= dataPrompt - 1; i++) { //loop to fill the array with numbers 
     nums[i] = prompt("Enter a number",""); 
     nums[i] = parseInt(nums[i]); 
     contents += nums[i] + " "; //variable that will be called to display contents 
     sum = sum + nums[i]; 
    } 
     avg = sum/nums.length; 
    for(i = 0; i < nums.length; i++) { //loop to find the largest number 
     var biggest = nums[0]; //why does this have to be index 0 and not 'i' ? 
     if(nums[i] > biggest) 
     biggest = nums[i]; //largest integer in array 
    } 
    for(i = 0; i < nums.length; i++) { //loop to find smallest integer 
     var smallest = nums[0]; //why does this have to be the index 0 and not 'i' ?? 
     if(nums[i] < smallest) 
     smallest = nums[i]; //smallest integer in array 
    }  
    for(count = 0; count < nums.length; count++) { //count of numbers higher than average 
     if(nums[i] > avg) 
     count = nums[i]; 
    } 
    for(count2 = 0; count2 < nums.length; count2++) { //count of numbers lower than average 
     if(nums[i] < avg) 
     count2 = nums[i]; 
    } 
} 
+1

不确定,但对于(i = 0; i <= dataPrompt - 1; i ++)应该没有-1并将<=改为< – 2015-04-01 14:39:10

+0

看起来您对'var smallest'有一些范围问题,它已设置每次运行for循环。我不确定你在 – 2015-04-01 14:39:53

+3

最小和最大值应该设置为nums [0],但在for循环之前。 – jcubic 2015-04-01 14:39:55

回答

2

,因为要分配countcount2 inccorectly您的功能没有返回正确的价值观。如果您在末尾运行代码countcount2将等于nums.length。这是因为您在for循环中使用它们。以及在你参考i,这是(我相信)在这一点上也等于nums.length

我想你想是这样的:

count = 0; 
count2 = 0; 

for(i = 0; i < nums.length; i++) 
{ 
    if(nums[i] > avg) 
    { 
     count++; //Increase the count of numbers above the average 
    } 
    else if(nums[i] < avg) 
    { 
     count2++; //Increase the count of numbers below the average 
    } 
} 

您可能希望你似乎对他们有点糊涂做对scope一些阅读和for循环。

编辑

如果你想在阵列中的最大和最小值,你可以做这样的事情:

//Assign the values to the first element by default 
var biggest = nums[0]; 
var smallest = nums[0]; 

for(var i = 1; i < nums.length; i++) 
{ 
    //Set biggest to the larger number, either biggest or the current number 
    biggest = Math.max(biggest, nums[i]); 
    //Set smallest to the smaller number, either biggest or the current number 
    smallest = Math.min(smallest, nums[i]); 
} 

注:这里假设你的数组

中至少有1个值
+0

最初我试图用'for'循环对其进行编码,但在这里搜索时遇到了Math.max方法。这是实现正确输出的最有效方式。此外,忽视了柜台应更新以保持计数的事实。欣赏评论。对于循环和嵌套循环给我适合......难以遵循并真正理解它们。 – allendks45 2015-04-01 15:05:29

+0

对于循环和嵌套循环需要一些习惯。我发现其中一件事情是通过一些小的输入在纸上运行您的代码(确保您只执行代码中写的内容)。这通常有助于发现错误 – 2015-04-01 15:40:43

相关问题