Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的函数不能返回准确的计数?_Javascript_Arrays - Fatal编程技术网

Javascript 为什么我的函数不能返回准确的计数?

Javascript 为什么我的函数不能返回准确的计数?,javascript,arrays,Javascript,Arrays,扩展代码我一直在为一个补充跟踪器工作,但我当前的函数不是返回大于平均值“的准确数字计数,也不是返回低于平均值的整数计数。我还注释掉了代码中的两个问题,因为我不太明白为什么数组被设置为索引[0]。我从这里的评论和寻找答案中学到了很多。所以感谢这个网站的存在!希望通过这个问题能学到更多 function suppArray() { var nums = new Array(); //create array var sum = 0; //variable to hold sum of intege

扩展代码我一直在为一个补充跟踪器工作,但我当前的函数不是返回大于平均值“的准确数字计数,也不是返回低于平均值的整数计数。我还注释掉了代码中的两个问题,因为我不太明白为什么数组被设置为索引[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];
    }
}
函数数组(){
var nums=new Array();//创建数组
var sum=0;//用于保存数组中整数和的变量
var avg=0;//用于保存平均值的变量
var i;
var计数=0;
var count2=0;
var contents='';//用于保存输出内容的变量
var dataPrompt=prompt(“您要输入多少个数字?”,“”);
dataPrompt=parseInt(dataPrompt);
对于(i=0;i最大)
最大=nums[i];//数组中的最大整数
}
对于(i=0;i平均值)
计数=nums[i];
}   
对于(count2=0;count2
您的函数没有返回正确的值,因为您正在正确分配
count
count2
。如果在末尾运行代码,
count
count2
将等于
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
    }
}   
//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]);
}
count=0;
count2=0;
对于(i=0;i平均值)
{
count++;//将数字的计数增加到平均值以上
}
否则如果(nums[i]
你可能想读一些关于和循环的书,因为你似乎对它们有点困惑

编辑

如果需要数组中的最大值和最小值,可以执行以下操作:

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
    }
}   
//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]);
}
//默认情况下,将值分配给第一个元素
var最大值=nums[0];
var最小值=nums[0];
对于(变量i=1;i

注意:这假设数组中至少有1个值

不确定,但对于(i=0;i)来说,
var minimate
似乎存在一些范围问题,它是在每次运行for循环时设置的。我不确定您试图实现什么,因此最小值和最大值应设置为nums[0]但在for循环之前。您正在计算数字,所以请改为执行
count++
count2++
。分配在那里没有意义。最初,我试图仅使用“for”循环对其进行编码,但在搜索时遇到了Math.max方法。这是获得正确输出的最有效方法。此外,在我知道计数器应该更新以保持计数,这也很有效。感谢您的评论。For循环和嵌套循环给了我适合的感觉…很难理解和真正理解它们。For循环和嵌套循环需要一点时间来适应。我发现方便的一件事是在纸上运行代码(确保你只做代码中写的事情)用一些小的输入。这通常有助于发现错误