2015-04-01 105 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

相關問題